-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtest_raw.py
1880 lines (1514 loc) · 79.8 KB
/
test_raw.py
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import ctypes
import ctypes.util
import os
import socket
import sys
import unittest
import gssapi.raw as gb
import gssapi.raw.misc as gbmisc
import k5test.unit as ktu
import k5test as kt
from collections.abc import Set
TARGET_SERVICE_NAME = b'host'
FQDN = (
'localhost' if sys.platform == 'darwin' else socket.getfqdn()
).encode('utf-8')
SERVICE_PRINCIPAL = TARGET_SERVICE_NAME + b'/' + FQDN
if sys.platform == 'darwin':
TARGET_SERVICE_NAME += b"@" + FQDN
class _GSSAPIKerberosTestCase(kt.KerberosTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
svc_princ = SERVICE_PRINCIPAL.decode("UTF-8")
cls.realm.kinit(svc_princ, flags=['-k'])
cls._init_env()
cls.USER_PRINC = cls.realm.user_princ.split('@')[0].encode("UTF-8")
cls.ADMIN_PRINC = cls.realm.admin_princ.split('@')[0].encode("UTF-8")
cls.KRB5_LIB_PATH = os.environ.get("GSSAPI_KRB5_MAIN_LIB", None)
@classmethod
def _init_env(cls):
cls._saved_env = copy.deepcopy(os.environ)
for k, v in cls.realm.env.items():
os.environ[k] = v
@classmethod
def _restore_env(cls):
for k in copy.deepcopy(os.environ):
if k in cls._saved_env:
os.environ[k] = cls._saved_env[k]
else:
del os.environ[k]
cls._saved_env = None
@classmethod
def tearDownClass(cls):
super().tearDownClass()
cls._restore_env()
class TestBaseUtilities(_GSSAPIKerberosTestCase):
def setUp(self):
self.realm.kinit(SERVICE_PRINCIPAL.decode("UTF-8"), flags=['-k'])
def test_indicate_mechs(self):
mechs = gb.indicate_mechs()
self.assertIsInstance(mechs, set)
self.assertIn(gb.MechType.kerberos, mechs)
def test_import_name(self):
imported_name = gb.import_name(TARGET_SERVICE_NAME)
self.assertIsInstance(imported_name, gb.Name)
gb.release_name(imported_name)
def test_canonicalize_export_name(self):
imported_name = gb.import_name(self.ADMIN_PRINC,
gb.NameType.kerberos_principal)
canonicalized_name = gb.canonicalize_name(imported_name,
gb.MechType.kerberos)
self.assertIsInstance(canonicalized_name, gb.Name)
exported_name = gb.export_name(canonicalized_name)
self.assertIsInstance(exported_name, bytes)
self.assertGreater(len(exported_name), 0)
def test_duplicate_name(self):
orig_name = gb.import_name(TARGET_SERVICE_NAME)
new_name = gb.duplicate_name(orig_name)
self.assertIsNotNone(new_name)
self.assertTrue(gb.compare_name(orig_name, new_name))
def test_display_name(self):
imported_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
displ_resp = gb.display_name(imported_name)
self.assertIsNotNone(displ_resp)
displayed_name, out_type = displ_resp
self.assertIsInstance(displayed_name, bytes)
self.assertEqual(displayed_name, TARGET_SERVICE_NAME)
self.assertEqual(out_type, gb.NameType.hostbased_service)
# NB(directxman12): we don't test display_name_ext because the krb5 mech
# doesn't actually implement it
@ktu.gssapi_extension_test('rfc6680', 'RFC 6680')
@ktu.krb_provider_test(['mit'], 'Heimdal does not implemented for krb5')
def test_inquire_name_not_mech_name(self):
base_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
inquire_res = gb.inquire_name(base_name)
self.assertIsNotNone(inquire_res)
self.assertFalse(inquire_res.is_mech_name)
self.assertIsNone(inquire_res.mech)
@ktu.gssapi_extension_test('rfc6680', 'RFC 6680')
@ktu.krb_provider_test(['mit'], 'Heimdal does not implemented for krb5')
def test_inquire_name_mech_name(self):
base_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
mech_name = gb.canonicalize_name(base_name, gb.MechType.kerberos)
inquire_res = gb.inquire_name(mech_name)
self.assertIsNotNone(inquire_res)
self.assertTrue(inquire_res.is_mech_name)
self.assertIsInstance(inquire_res.mech, gb.OID)
self.assertEqual(inquire_res.mech, gb.MechType.kerberos)
@ktu.gssapi_extension_test('rfc6680', 'RFC 6680')
@ktu.gssapi_extension_test('rfc6680_comp_oid',
'RFC 6680 (COMPOSITE_EXPORT OID)')
def test_import_export_name_composite_no_attrs(self):
base_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
canon_name = gb.canonicalize_name(base_name,
gb.MechType.kerberos)
exported_name = gb.export_name_composite(canon_name)
self.assertIsInstance(exported_name, bytes)
imported_name = gb.import_name(exported_name,
gb.NameType.composite_export)
self.assertIsInstance(imported_name, gb.Name)
# NB(directxman12): the greet_client plugin only allows for one value
@ktu.gssapi_extension_test('rfc6680', 'RFC 6680')
@ktu.krb_plugin_test('authdata', 'greet_client')
def test_inquire_name_with_attrs(self):
base_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
canon_name = gb.canonicalize_name(base_name, gb.MechType.kerberos)
gb.set_name_attribute(canon_name, b'urn:greet:greeting',
[b'some greeting'])
inquire_res = gb.inquire_name(canon_name)
self.assertIsInstance(inquire_res.attrs, list)
self.assertEqual(inquire_res.attrs, [b"urn:greet:greeting"])
@ktu.gssapi_extension_test('rfc6680', 'RFC 6680')
@ktu.krb_plugin_test('authdata', 'greet_client')
def test_basic_get_set_delete_name_attributes_no_auth(self):
base_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
canon_name = gb.canonicalize_name(base_name, gb.MechType.kerberos)
gb.set_name_attribute(canon_name, b'urn:greet:greeting',
[b'some other val'], complete=True)
get_res = gb.get_name_attribute(canon_name, b'urn:greet:greeting')
self.assertIsNotNone(get_res)
self.assertIsInstance(get_res.values, list)
self.assertEqual(get_res.values, [b"some other val"])
self.assertIsInstance(get_res.display_values, list)
self.assertEqual(get_res.display_values, get_res.values)
self.assertTrue(get_res.complete)
self.assertFalse(get_res.authenticated)
gb.delete_name_attribute(canon_name, b'urn:greet:greeting')
# NB(directxman12): the code below currently segfaults due to the way
# that krb5 and the krb5 greet plugin is written
# gb.get_name_attribute.should_raise(
# gb.exceptions.OperationUnavailableError, canon_name,
# 'urn:greet:greeting')
@ktu.gssapi_extension_test('rfc6680', 'RFC 6680')
@ktu.krb_plugin_test('authdata', 'greet_client')
def test_import_export_name_composite(self):
base_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
canon_name = gb.canonicalize_name(base_name, gb.MechType.kerberos)
gb.set_name_attribute(canon_name, b'urn:greet:greeting', [b'some val'])
exported_name = gb.export_name_composite(canon_name)
self.assertIsInstance(exported_name, bytes)
# TODO(directxman12): when you just import a token as composite,
# appears as this name whose text is all garbled, since it contains
# all of the attributes, etc, but doesn't properly have the attributes.
# Once it's canonicalized, the attributes reappear. However, if you
# just import it as normal export, the attributes appear directly.
# It is thus unclear as to what is going on
# imported_name_raw = gb.import_name(exported_name,
# gb.NameType.composite_export)
# imported_name = gb.canonicalize_name(imported_name_r,
# gb.MechType.kerberos)
imported_name = gb.import_name(exported_name, gb.NameType.export)
self.assertIsInstance(imported_name, gb.Name)
get_res = gb.get_name_attribute(imported_name, b'urn:greet:greeting')
self.assertEqual(get_res.values, [b"some val"])
def test_compare_name(self):
service_name1 = gb.import_name(TARGET_SERVICE_NAME)
service_name2 = gb.import_name(TARGET_SERVICE_NAME)
init_name = gb.import_name(self.ADMIN_PRINC,
gb.NameType.kerberos_principal)
self.assertTrue(gb.compare_name(service_name1, service_name2))
self.assertTrue(gb.compare_name(service_name2, service_name1))
self.assertFalse(gb.compare_name(service_name1, init_name))
gb.release_name(service_name1)
gb.release_name(service_name2)
gb.release_name(init_name)
def test_display_status(self):
status_resp = gbmisc._display_status(0, False)
self.assertIsNotNone(status_resp)
status, ctx, cont = status_resp
self.assertIsInstance(status, bytes)
self.assertGreater(len(status), 0)
self.assertIsInstance(ctx, int)
self.assertIsInstance(cont, bool)
self.assertFalse(cont)
def test_acquire_creds(self):
name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
cred_resp = gb.acquire_cred(name)
self.assertIsNotNone(cred_resp)
creds, actual_mechs, ttl = cred_resp
self.assertIsInstance(creds, gb.Creds)
self.assertIn(gb.MechType.kerberos, actual_mechs)
if sys.platform != 'darwin':
self.assertIsInstance(ttl, int)
gb.release_name(name)
gb.release_cred(creds)
@ktu.gssapi_extension_test('cred_imp_exp', 'credentials import-export')
def test_cred_import_export(self):
creds = gb.acquire_cred(None).creds
token = gb.export_cred(creds)
imported_creds = gb.import_cred(token)
inquire_orig = gb.inquire_cred(creds, name=True)
inquire_imp = gb.inquire_cred(imported_creds, name=True)
self.assertTrue(gb.compare_name(inquire_orig.name, inquire_imp.name))
def test_context_time(self):
target_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
ctx_resp = gb.init_sec_context(target_name)
client_token1 = ctx_resp[3]
client_ctx = ctx_resp[0]
server_name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
server_creds = gb.acquire_cred(server_name)[0]
server_resp = gb.accept_sec_context(client_token1,
acceptor_creds=server_creds)
server_tok = server_resp[3]
client_resp2 = gb.init_sec_context(target_name,
context=client_ctx,
input_token=server_tok)
ctx = client_resp2[0]
ttl = gb.context_time(ctx)
self.assertIsInstance(ttl, int)
self.assertGreater(ttl, 0)
def test_inquire_context(self):
target_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
ctx_resp = gb.init_sec_context(target_name)
client_token1 = ctx_resp[3]
client_ctx = ctx_resp[0]
server_name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
server_creds = gb.acquire_cred(server_name)[0]
server_resp = gb.accept_sec_context(client_token1,
acceptor_creds=server_creds)
server_tok = server_resp[3]
client_resp2 = gb.init_sec_context(target_name,
context=client_ctx,
input_token=server_tok)
ctx = client_resp2[0]
inq_resp = gb.inquire_context(ctx)
self.assertIsNotNone(inq_resp)
src_name, target_name, ttl, mech_type, flags, local_est, is_open = \
inq_resp
self.assertIsInstance(src_name, gb.Name)
self.assertIsInstance(target_name, gb.Name)
self.assertIsInstance(ttl, int)
self.assertEqual(mech_type, gb.MechType.kerberos)
self.assertIsInstance(flags, Set)
self.assertGreater(len(flags), 0)
self.assertIsInstance(local_est, bool)
self.assertTrue(local_est)
self.assertIsInstance(is_open, bool)
self.assertTrue(is_open)
# NB(directxman12): We don't test `process_context_token` because
# there is no clear non-deprecated way to test it
@ktu.gssapi_extension_test('s4u', 'S4U')
def test_add_cred_impersonate_name(self):
server_name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
password = self.realm.password('user')
self.realm.kinit(self.realm.user_princ, password=password,
flags=["-f"])
name = gb.import_name(b"user", gb.NameType.kerberos_principal)
client_creds = gb.acquire_cred(name, usage="initiate").creds
cctx_res = gb.init_sec_context(
server_name, creds=client_creds,
flags=gb.RequirementFlag.delegate_to_peer)
self.realm.kinit(SERVICE_PRINCIPAL.decode("utf-8"), flags=["-k"])
server_creds = gb.acquire_cred(server_name, usage="both").creds
sctx_res = gb.accept_sec_context(cctx_res.token, server_creds)
self.assertTrue(gb.inquire_context(sctx_res.context).complete)
input_creds = gb.Creds()
imp_resp = gb.add_cred_impersonate_name(input_creds,
sctx_res.delegated_creds,
server_name,
gb.MechType.kerberos)
self.assertIsNotNone(imp_resp)
self.assertIsInstance(imp_resp, gb.AddCredResult)
self.assertIsInstance(imp_resp.creds, gb.Creds)
self.assertIn(gb.MechType.kerberos, imp_resp.mechs)
self.assertIsInstance(imp_resp.init_lifetime, int)
self.assertGreater(imp_resp.init_lifetime, 0)
self.assertIsInstance(imp_resp.accept_lifetime, int)
self.assertEqual(imp_resp.accept_lifetime, 0)
@ktu.gssapi_extension_test('s4u', 'S4U')
def test_acquire_creds_impersonate_name(self):
server_name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
password = self.realm.password('user')
self.realm.kinit(self.realm.user_princ, password=password,
flags=["-f"])
name = gb.import_name(b'user', gb.NameType.kerberos_principal)
client_creds = gb.acquire_cred(name, usage="initiate").creds
cctx_res = gb.init_sec_context(
server_name, creds=client_creds,
flags=gb.RequirementFlag.delegate_to_peer)
self.realm.kinit(SERVICE_PRINCIPAL.decode("utf-8"), flags=["-k"])
server_creds = gb.acquire_cred(server_name, usage='both').creds
sctx_res = gb.accept_sec_context(cctx_res.token, server_creds)
self.assertTrue(gb.inquire_context(sctx_res.context).complete)
imp_resp = gb.acquire_cred_impersonate_name(sctx_res.delegated_creds,
server_name)
self.assertIsInstance(imp_resp, gb.AcquireCredResult)
self.assertIsInstance(imp_resp.creds, gb.Creds)
self.assertIn(gb.MechType.kerberos, imp_resp.mechs)
self.assertIsInstance(imp_resp.lifetime, int)
self.assertGreater(imp_resp.lifetime, 0)
@ktu.gssapi_extension_test('s4u', 'S4U')
@ktu.krb_minversion_test('1.11',
'returning delegated S4U2Proxy credentials',
provider='mit')
def test_always_get_delegated_creds(self):
svc_princ = SERVICE_PRINCIPAL.decode("UTF-8")
self.realm.kinit(svc_princ, flags=['-k', '-f'])
target_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
client_token = gb.init_sec_context(target_name).token
# if our acceptor creds have a usage of both, we get
# s4u2proxy delegated credentials
server_creds = gb.acquire_cred(None, usage='both').creds
server_ctx_resp = gb.accept_sec_context(client_token,
acceptor_creds=server_creds)
self.assertIsNotNone(server_ctx_resp)
self.assertIsInstance(server_ctx_resp.delegated_creds, gb.Creds)
@ktu.gssapi_extension_test('rfc5588', 'RFC 5588')
def test_store_cred_acquire_cred(self):
# we need to acquire a forwardable ticket
svc_princ = SERVICE_PRINCIPAL.decode("UTF-8")
self.realm.kinit(svc_princ, flags=['-k', '-f'])
target_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
client_creds = gb.acquire_cred(None, usage='initiate').creds
client_ctx_resp = gb.init_sec_context(
target_name, creds=client_creds,
flags=gb.RequirementFlag.delegate_to_peer)
client_token = client_ctx_resp[3]
server_creds = gb.acquire_cred(None, usage='accept').creds
server_ctx_resp = gb.accept_sec_context(client_token,
acceptor_creds=server_creds)
deleg_creds = server_ctx_resp.delegated_creds
self.assertIsNotNone(deleg_creds)
store_res = gb.store_cred(deleg_creds, usage='initiate',
mech=gb.MechType.kerberos,
set_default=True, overwrite=True)
self.assertIsNotNone(store_res)
if self.realm.provider.lower() != 'heimdal':
# Heimdal does not return this info as expected
self.assertEqual(store_res.usage, "initiate")
self.assertIn(gb.MechType.kerberos, store_res.mechs)
deleg_name = gb.inquire_cred(deleg_creds).name
acq_resp = gb.acquire_cred(deleg_name, usage='initiate')
self.assertIsNotNone(acq_resp)
@ktu.gssapi_extension_test('cred_store', 'credentials store')
def test_store_cred_into_acquire_cred(self):
CCACHE = f'FILE:{self.realm.tmpdir}/other_ccache'
KT = f'{self.realm.tmpdir}/other_keytab'
store = {b'ccache': CCACHE.encode('UTF-8'),
b'keytab': KT.encode('UTF-8')}
princ_name = 'service/cs@' + self.realm.realm
self.realm.addprinc(princ_name)
self.realm.extract_keytab(princ_name, KT)
self.realm.kinit(princ_name, None, ['-k', '-t', KT])
initial_creds = gb.acquire_cred(None, usage='initiate').creds
# NB(sross): overwrite because the ccache doesn't exist yet
expected_usage = 'initiate'
store_kwargs = {}
if self.realm.provider.lower() == 'heimdal':
expected_usage = 'both'
store_kwargs['mech'] = gb.MechType.kerberos
store_kwargs['usage'] = 'initiate'
store_res = gb.store_cred_into(store, initial_creds, overwrite=True,
**store_kwargs)
self.assertIsNotNone(store_res.mechs)
self.assertEqual(store_res.usage, expected_usage)
name = gb.import_name(princ_name.encode('UTF-8'))
retrieve_res = gb.acquire_cred_from(store, name)
self.assertIsNotNone(retrieve_res)
self.assertIsNotNone(retrieve_res.creds)
self.assertIsInstance(retrieve_res.creds, gb.Creds)
self.assertIn(gb.MechType.kerberos, retrieve_res.mechs)
self.assertIsInstance(retrieve_res.lifetime, int)
def test_add_cred(self):
if sys.platform == 'darwin':
self.skipTest('macOS fails to find the credential')
target_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
client_ctx_resp = gb.init_sec_context(target_name)
client_token = client_ctx_resp[3]
del client_ctx_resp # free all the things (except the token)!
server_name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
server_creds = gb.acquire_cred(server_name, usage='both')[0]
server_ctx_resp = gb.accept_sec_context(client_token,
acceptor_creds=server_creds)
input_creds = gb.Creds()
imp_resp = gb.add_cred(input_creds,
server_ctx_resp[1],
gb.MechType.kerberos)
self.assertIsNotNone(imp_resp)
new_creds, actual_mechs, output_init_ttl, output_accept_ttl = imp_resp
self.assertIsInstance(new_creds, gb.Creds)
self.assertIn(gb.MechType.kerberos, actual_mechs)
self.assertIsInstance(output_init_ttl, int)
self.assertIsInstance(output_accept_ttl, int)
# NB(sross): we skip testing add_cred with mutate for the same reasons
# that testing add_cred in the high-level API is skipped
def test_inquire_creds(self):
name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
cred = gb.acquire_cred(name).creds
inq_resp = gb.inquire_cred(cred)
self.assertIsNotNone(inq_resp)
self.assertIsInstance(inq_resp.name, gb.Name)
if self.realm.provider.lower() == 'heimdal':
name = gb.import_name(self.realm.host_princ.encode('utf-8'),
gb.NameType.kerberos_principal)
self.assertTrue(gb.compare_name(name, inq_resp.name))
if sys.platform == 'darwin':
self.assertEqual(inq_resp.usage, "accept")
else:
self.assertIsInstance(inq_resp.lifetime, int)
self.assertEqual(inq_resp.usage, "both")
self.assertIn(gb.MechType.kerberos, inq_resp.mechs)
def test_create_oid_from_bytes(self):
kerberos_bytes = gb.MechType.kerberos.__bytes__()
new_oid = gb.OID(elements=kerberos_bytes)
self.assertEqual(new_oid, gb.MechType.kerberos)
del new_oid # make sure we can dealloc
def test_error_dispatch(self):
err_code1 = gb.ParameterReadError.CALLING_CODE
err_code2 = gb.BadNameError.ROUTINE_CODE
err = gb.GSSError(err_code1 | err_code2, 0)
self.assertIsInstance(err, gb.NameReadError)
self.assertEqual(err.maj_code, err_code1 | err_code2)
def test_inquire_names_for_mech(self):
res = gb.inquire_names_for_mech(gb.MechType.kerberos)
self.assertIsNotNone(res)
self.assertIn(gb.NameType.kerberos_principal, res)
def test_inquire_mechs_for_name(self):
name = gb.import_name(self.USER_PRINC,
gb.NameType.kerberos_principal)
res = gb.inquire_mechs_for_name(name)
self.assertIsNotNone(res)
self.assertIn(gb.MechType.kerberos, res)
@ktu.gssapi_extension_test('password', 'Password')
def test_acquire_cred_with_password(self):
password = self.realm.password('user')
self.realm.kinit(self.realm.user_princ, password=password)
name = gb.import_name(b'user', gb.NameType.kerberos_principal)
imp_resp = gb.acquire_cred_with_password(name,
password.encode('UTF-8'))
self.assertIsNotNone(imp_resp)
imp_creds, actual_mechs, output_ttl = imp_resp
self.assertIsNotNone(imp_creds)
self.assertIsInstance(imp_creds, gb.Creds)
if sys.platform == 'darwin':
self.assertIn(gb.OID.from_int_seq('1.3.6.1.5.2.5'), actual_mechs)
else:
self.assertIn(gb.MechType.kerberos, actual_mechs)
self.assertIsInstance(output_ttl, int)
@ktu.gssapi_extension_test('password_add', 'Password (add)')
def test_add_cred_with_password(self):
password = self.realm.password('user')
self.realm.kinit(self.realm.user_princ, password=password)
name = gb.import_name(b'user', gb.NameType.kerberos_principal)
input_creds = gb.Creds()
imp_resp = gb.add_cred_with_password(input_creds, name,
gb.MechType.kerberos,
password.encode('UTF-8'))
self.assertIsNotNone(imp_resp)
new_creds, actual_mechs, output_init_ttl, output_accept_ttl = imp_resp
self.assertIsInstance(new_creds, gb.Creds)
self.assertIn(gb.MechType.kerberos, actual_mechs)
self.assertIsInstance(output_init_ttl, int)
self.assertIsInstance(output_accept_ttl, int)
@ktu.gssapi_extension_test('rfc5587', 'RFC 5587')
def test_rfc5587(self):
if sys.platform == "darwin":
self.skipTest("too many edge cases on macOS")
mechs = gb.indicate_mechs_by_attrs(None, None, None)
self.assertIsInstance(mechs, set)
self.assertGreater(len(mechs), 0)
# We're validating RFC 5587 here: by iterating over all mechanisms,
# we can query their attributes and build a mapping of attr->{mechs}.
# To test indicate_mechs_by_attrs, we can use this mapping and
# ensure that, when the attribute is placed in a slot, we get the
# expected result (e.g., attr in have --> mechs are present).
attrs_dict = {}
known_attrs_dict = {}
for mech in mechs:
self.assertIsInstance(mech, gb.OID)
inquire_out = gb.inquire_attrs_for_mech(mech)
mech_attrs = inquire_out.mech_attrs
known_mech_attrs = inquire_out.known_mech_attrs
self.assertIsInstance(mech_attrs, set)
self.assertIsInstance(known_mech_attrs, set)
# Verify that we get data for every available
# attribute. Testing the contents of a few known
# attributes is done in test_display_mech_attr().
for mech_attr in mech_attrs:
self.assertIsInstance(mech_attr, gb.OID)
display_out = gb.display_mech_attr(mech_attr)
self.assertIsInstance(display_out.name, bytes)
self.assertIsInstance(display_out.short_desc, bytes)
self.assertIsInstance(display_out.long_desc, bytes)
if mech_attr not in attrs_dict:
attrs_dict[mech_attr] = set()
attrs_dict[mech_attr].add(mech)
for mech_attr in known_mech_attrs:
self.assertIsInstance(mech_attr, gb.OID)
display_out = gb.display_mech_attr(mech_attr)
self.assertIsInstance(display_out.name, bytes)
self.assertIsInstance(display_out.short_desc, bytes)
self.assertIsInstance(display_out.long_desc, bytes)
if mech_attr not in known_attrs_dict:
known_attrs_dict[mech_attr] = set()
known_attrs_dict[mech_attr].add(mech)
for attr, expected_mechs in attrs_dict.items():
attrs = {attr}
mechs = gb.indicate_mechs_by_attrs(attrs, None, None)
self.assertGreater(len(mechs), 0)
self.assertEqual(mechs, expected_mechs)
mechs = gb.indicate_mechs_by_attrs(None, attrs, None)
for expected_mech in expected_mechs:
self.assertNotIn(expected_mech, mechs)
if self.realm.provider.lower() != 'heimdal':
# Heimdal doesn't fully implement gss_indicate_mechs_by_attrs
for attr, expected_mechs in known_attrs_dict.items():
attrs = {attr}
mechs = gb.indicate_mechs_by_attrs(None, None, attrs)
self.assertGreater(len(mechs), 0)
self.assertEqual(mechs, expected_mechs)
@ktu.gssapi_extension_test('rfc5587', 'RFC 5587')
def test_display_mech_attr(self):
test_attrs = [
# oid, name, short_desc, long_desc
# Taken from krb5/src/tests/gssapi/t_saslname
[gb.OID.from_int_seq("1.3.6.1.5.5.13.24"),
b"GSS_C_MA_CBINDINGS", b"channel-bindings",
b"Mechanism supports channel bindings."],
[gb.OID.from_int_seq("1.3.6.1.5.5.13.1"),
b"GSS_C_MA_MECH_CONCRETE", b"concrete-mech",
b"Mechanism is neither a pseudo-mechanism nor a composite "
b"mechanism."]
]
if self.realm.provider.lower() == 'heimdal':
test_attrs[0][3] = b""
test_attrs[1][3] = b"Indicates that a mech is neither a " \
b"pseudo-mechanism nor a composite mechanism"
for attr in test_attrs:
display_out = gb.display_mech_attr(attr[0])
self.assertEqual(display_out.name, attr[1])
self.assertEqual(display_out.short_desc, attr[2])
self.assertEqual(display_out.long_desc, attr[3])
@ktu.gssapi_extension_test('rfc5801', 'SASL Names')
def test_sasl_names(self):
mechs = gb.indicate_mechs()
for mech in mechs:
out = gb.inquire_saslname_for_mech(mech)
out_smn = out.sasl_mech_name
if out_smn:
self.assertIsInstance(out_smn, bytes)
self.assertGreater(len(out_smn), 0)
out_mn = out.mech_name
self.assertIsInstance(out_mn, bytes)
out_md = out.mech_description
self.assertIsInstance(out_md, bytes)
# Heimdal fails with Unknown mech-code on sanon
if not (self.realm.provider.lower() == 'heimdal' and
mech.dotted_form == '1.3.6.1.4.1.5322.26.1.110'):
cmp_mech = gb.inquire_mech_for_saslname(out_smn)
self.assertIsNotNone(cmp_mech)
# For some reason macOS sometimes returns this for mechs
if not (sys.platform == 'darwin' and
cmp_mech.dotted_form == '1.2.752.43.14.2'):
self.assertEqual(cmp_mech, mech)
@ktu.gssapi_extension_test('rfc4178', 'Negotiation Mechanism')
def test_set_neg_mechs(self):
all_mechs = gb.indicate_mechs()
spnego_mech = gb.OID.from_int_seq("1.3.6.1.5.5.2")
krb5_mech = gb.OID.from_int_seq("1.2.840.113554.1.2.2")
ntlm_mech = gb.OID.from_int_seq("1.3.6.1.4.1.311.2.2.10")
server_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
username = gb.import_name(name=b"user",
name_type=gb.NameType.user)
krb5_client_creds = gb.acquire_cred(
None, usage='initiate',
mechs=[krb5_mech, spnego_mech]).creds
try:
ntlm_client_creds = gb.acquire_cred_with_password(
name=username,
password=b'password',
mechs=[ntlm_mech, spnego_mech]).creds
except gb.GSSError:
self.skipTest('You do not have the GSSAPI gss-ntlmssp mech '
'installed')
server_creds = gb.acquire_cred(server_name, usage='accept',
mechs=all_mechs).creds
neg_resp = gb.set_neg_mechs(server_creds, [ntlm_mech])
self.assertIsNone(neg_resp)
client_ctx_resp = gb.init_sec_context(server_name,
creds=ntlm_client_creds,
mech=spnego_mech)
client_token = client_ctx_resp.token
server_ctx_resp = gb.accept_sec_context(client_token,
acceptor_creds=server_creds)
self.assertIsNotNone(server_ctx_resp)
client_ctx_resp = gb.init_sec_context(server_name,
creds=krb5_client_creds,
mech=spnego_mech)
client_token = client_ctx_resp.token
self.assertRaises(gb.GSSError, gb.accept_sec_context, client_token,
acceptor_creds=server_creds)
neg_resp = gb.set_neg_mechs(server_creds, [krb5_mech])
self.assertIsNone(neg_resp)
client_ctx_resp = gb.init_sec_context(server_name,
creds=krb5_client_creds,
mech=spnego_mech)
client_token = client_ctx_resp.token
server_ctx_resp = gb.accept_sec_context(client_token,
acceptor_creds=server_creds)
self.assertIsNotNone(server_ctx_resp)
client_ctx_resp = gb.init_sec_context(server_name,
creds=ntlm_client_creds,
mech=spnego_mech)
client_token = client_ctx_resp.token
self.assertRaises(gb.GSSError, gb.accept_sec_context, client_token,
acceptor_creds=server_creds)
@ktu.gssapi_extension_test('ggf', 'Global Grid Forum')
@ktu.gssapi_extension_test('s4u', 'S4U')
@ktu.krb_minversion_test('1.16',
'querying impersonator name of krb5 GSS '
'Credential using the '
'GSS_KRB5_GET_CRED_IMPERSONATOR OID',
provider='mit')
def test_inquire_cred_by_oid_impersonator(self):
svc_princ = SERVICE_PRINCIPAL.decode("UTF-8")
self.realm.kinit(svc_princ, flags=['-k', '-f'])
target_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
client_token = gb.init_sec_context(target_name).token
# if our acceptor creds have a usage of both, we get
# s4u2proxy delegated credentials
server_creds = gb.acquire_cred(None, usage='both').creds
server_ctx_resp = gb.accept_sec_context(client_token,
acceptor_creds=server_creds)
self.assertIsNotNone(server_ctx_resp)
self.assertIsNotNone(server_ctx_resp.delegated_creds)
self.assertIsInstance(server_ctx_resp.delegated_creds, gb.Creds)
# GSS_KRB5_GET_CRED_IMPERSONATOR
oid = gb.OID.from_int_seq("1.2.840.113554.1.2.2.5.14")
info = gb.inquire_cred_by_oid(server_ctx_resp.delegated_creds, oid)
self.assertIsInstance(info, list)
self.assertGreater(len(info), 0)
self.assertIsInstance(info[0], bytes)
self.assertEqual(info[0], b"%s@%s" % (
SERVICE_PRINCIPAL, self.realm.realm.encode('utf-8')))
@ktu.gssapi_extension_test('ggf', 'Global Grid Forum')
def test_inquire_sec_context_by_oid(self):
target_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
ctx_resp1 = gb.init_sec_context(target_name)
server_name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
server_creds = gb.acquire_cred(server_name)[0]
server_resp = gb.accept_sec_context(ctx_resp1[3],
acceptor_creds=server_creds)
server_ctx = server_resp[0]
server_tok = server_resp[3]
client_resp2 = gb.init_sec_context(target_name,
context=ctx_resp1[0],
input_token=server_tok)
client_ctx = client_resp2[0]
# GSS_C_INQ_SSPI_SESSION_KEY
session_key_oid = gb.OID.from_int_seq("1.2.840.113554.1.2.2.5.5")
client_key = gb.inquire_sec_context_by_oid(client_ctx, session_key_oid)
server_key = gb.inquire_sec_context_by_oid(server_ctx, session_key_oid)
self.assertIsInstance(client_key, list)
self.assertGreater(len(client_key), 0)
self.assertIsInstance(server_key, list)
self.assertGreater(len(server_key), 0)
self.assertCountEqual(client_key, server_key)
@ktu.gssapi_extension_test('ggf', 'Global Grid Forum')
def test_inquire_sec_context_by_oid_should_raise_error(self):
target_name = gb.import_name(TARGET_SERVICE_NAME,
gb.NameType.hostbased_service)
ctx_resp1 = gb.init_sec_context(target_name)
server_name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
server_creds = gb.acquire_cred(server_name)[0]
server_resp = gb.accept_sec_context(ctx_resp1[3],
acceptor_creds=server_creds)
client_resp2 = gb.init_sec_context(target_name,
context=ctx_resp1[0],
input_token=server_resp[3])
client_ctx = client_resp2[0]
invalid_oid = gb.OID.from_int_seq("1.2.3.4.5.6.7.8.9")
self.assertRaises(gb.GSSError, gb.inquire_sec_context_by_oid,
client_ctx, invalid_oid)
@ktu.gssapi_extension_test('ggf', 'Global Grid Forum')
@ktu.gssapi_extension_test('password', 'Add Credential with Password')
def test_set_sec_context_option(self):
if sys.platform == 'darwin':
self.skipTest("macOS NTLM does not implement this OID")
ntlm_mech = gb.OID.from_int_seq("1.3.6.1.4.1.311.2.2.10")
username = gb.import_name(name=b"user",
name_type=gb.NameType.user)
try:
cred = gb.acquire_cred_with_password(name=username,
password=b"password",
mechs=[ntlm_mech])
except gb.GSSError:
self.skipTest('You do not have the GSSAPI gss-ntlmssp mech '
'installed')
server = gb.import_name(name=b"server",
name_type=gb.NameType.hostbased_service)
orig_context = gb.init_sec_context(server, creds=cred.creds,
mech=ntlm_mech)[0]
# GSS_NTLMSSP_RESET_CRYPTO_OID_STRING
reset_mech = gb.OID.from_int_seq("1.3.6.1.4.1.7165.655.1.3")
out_context = gb.set_sec_context_option(reset_mech,
context=orig_context,
value=b"\x00" * 4)
self.assertIsInstance(out_context, gb.SecurityContext)
@ktu.gssapi_extension_test('ggf', 'Global Grid Forum')
@ktu.gssapi_extension_test('password', 'Add Credential with Password')
def test_set_sec_context_option_fail(self):
ntlm_mech = gb.OID.from_int_seq("1.3.6.1.4.1.311.2.2.10")
username = gb.import_name(name=b"user",
name_type=gb.NameType.user)
try:
cred = gb.acquire_cred_with_password(name=username,
password=b"password",
mechs=[ntlm_mech])
except gb.GSSError:
self.skipTest('You do not have the GSSAPI gss-ntlmssp mech '
'installed')
server = gb.import_name(name=b"server",
name_type=gb.NameType.hostbased_service)
context = gb.init_sec_context(server, creds=cred.creds,
mech=ntlm_mech)[0]
# GSS_NTLMSSP_RESET_CRYPTO_OID_STRING
reset_mech = gb.OID.from_int_seq("1.3.6.1.4.1.7165.655.1.3")
# will raise a GSSError if no data was passed in
self.assertRaises(gb.GSSError, gb.set_sec_context_option, reset_mech,
context)
@ktu.gssapi_extension_test('set_cred_opt', 'Kitten Set Credential Option')
@ktu.krb_minversion_test('1.14',
'GSS_KRB5_CRED_NO_CI_FLAGS_X was added in MIT '
'krb5 1.14', provider='mit')
def test_set_cred_option(self):
name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
# GSS_KRB5_CRED_NO_CI_FLAGS_X
no_ci_flags_x = gb.OID.from_int_seq("1.2.752.43.13.29")
orig_cred = gb.acquire_cred(name).creds
# nothing much we can test here apart from it doesn't fail and the
# id of the return cred is the same as the input one
output_cred = gb.set_cred_option(no_ci_flags_x, creds=orig_cred)
self.assertIsInstance(output_cred, gb.Creds)
@ktu.gssapi_extension_test('set_cred_opt', 'Kitten Set Credential Option')
def test_set_cred_option_should_raise_error(self):
name = gb.import_name(SERVICE_PRINCIPAL,
gb.NameType.kerberos_principal)
orig_cred = gb.acquire_cred(name).creds
# this is a fake OID and shouldn't work at all
invalid_oid = gb.OID.from_int_seq("1.2.3.4.5.6.7.8.9")
self.assertRaises(gb.GSSError, gb.set_cred_option, invalid_oid,
orig_cred, b"\x00")
@ktu.gssapi_extension_test('krb5', 'Kerberos Extensions')
@ktu.krb_provider_test(['mit'], 'Cannot revert ccache on Heimdal')
# https://github.com/heimdal/heimdal/issues/803
def test_krb5_ccache_name(self):
provider = self.realm.provider.lower()
new_ccache = os.path.join(self.realm.tmpdir, 'ccache-new')
new_env = self.realm.env.copy()
new_env['KRB5CCNAME'] = new_ccache
self.realm.kinit(self.realm.user_princ,
password=self.realm.password('user'),
env=new_env)
old_ccache = gb.krb5_ccache_name(new_ccache.encode('utf-8'))
try:
if provider == 'heimdal':
# Heimdal never returns the old name - see above link
self.assertTrue(old_ccache is None)
else:
self.assertEqual(old_ccache.decode('utf-8'), self.realm.ccache)
cred_resp = gb.acquire_cred(usage='initiate').creds
princ_name = gb.inquire_cred(cred_resp, name=True).name
name = gb.display_name(princ_name, name_type=False).name
self.assertEqual(name, self.realm.user_princ.encode('utf-8'))
if provider != 'heimdal':