/
sspi.cpp
1620 lines (1462 loc) · 48.3 KB
/
sspi.cpp
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
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// This library is client-side only, and only supports the default credentials.
// It speaks krb5 and SPNEGO. NTLM is excluded from SPNEGO negotiation.
//
// This library can be built directly with the following command:
// cl -I %OPENJDK%\src\java.security.jgss\share\native\libj2gss\ sspi.cpp \
// -link -dll -out:sspi_bridge.dll
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <Strsafe.h>
#include <ntsecapi.h>
#include <new>
#define GSS_DLL_FILE
#include <gssapi.h>
#define SECURITY_WIN32
#include <sspi.h>
#pragma comment(lib, "secur32.lib")
// Otherwise an exception will be thrown
#define new new (std::nothrow)
// A debugging macro
#define PP(fmt, ...) \
if (trace) { \
fprintf(stderr, "[SSPI:%ld] "fmt"\n", __LINE__, ##__VA_ARGS__); \
fflush(stderr); \
}
#define SEC_SUCCESS(status) ((*minor_status = (status)), (status) >= SEC_E_OK)
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
// When SSPI_BRIDGE_TRACE is set, debug info goes to stderr. The value is ignored.
char* trace = getenv("SSPI_BRIDGE_TRACE");
void
dump(const char* title, PBYTE data, size_t len)
{
if (trace) {
fprintf(stderr, "==== %s ====\n", title);
for (size_t i = 0; i < len; i++) {
if (i != 0 && i % 16 == 0) {
fprintf(stderr, "\n");
}
fprintf(stderr, "%02X ", *(data + i) & 0xff);
}
fprintf(stderr, "\n");
}
}
gss_OID_desc KRB5_OID = {9, (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"};
gss_OID_desc SPNEGO_OID = {6, (void*)"\x2b\x06\x01\x05\x05\x02"};
gss_OID_desc USER_NAME_OID = {10, (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x01"};
gss_OID_desc KRB5_NAME_OID = {10, (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x01"};
gss_OID_desc HOST_SERVICE_NAME_OID = {10, (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
gss_OID_desc EXPORT_NAME_OID = {6, (void*)"\x2b\x06\x01\x05\x06\x04"};
struct gss_name_struct {
SEC_WCHAR* name;
};
struct gss_ctx_id_struct {
CredHandle* phCred;
CtxtHandle hCtxt;
SecPkgContext_Sizes SecPkgContextSizes;
SecPkgContext_NativeNames nnames;
BOOLEAN established;
BOOLEAN isSPNEGO;
BOOLEAN isLocalCred;
OM_uint32 flags;
};
struct gss_cred_id_struct {
CredHandle* phCredK;
CredHandle* phCredS;
long time;
};
/* This section holds supporting functions that are not exported */
static OM_uint32
seconds_until(int inputIsUTC, TimeStamp *time)
{
// time is local time
LARGE_INTEGER uiLocal;
FILETIME now;
GetSystemTimeAsFileTime(&now);
if (!inputIsUTC) {
FILETIME nowLocal;
if (FileTimeToLocalFileTime(&now, &nowLocal) == 0) {
return -1;
}
now = nowLocal;
}
uiLocal.HighPart = now.dwHighDateTime;
uiLocal.LowPart = now.dwLowDateTime;
if (time->QuadPart < uiLocal.QuadPart) {
return 0;
}
ULONGLONG diff = (time->QuadPart - uiLocal.QuadPart) / 10000000;
if (diff > (ULONGLONG)~(OM_uint32)0)
return GSS_C_INDEFINITE;
return (OM_uint32)diff;
}
static void
show_time(char* label, TimeStamp* ts)
{
if (trace) {
SYSTEMTIME stLocal;
FileTimeToSystemTime((FILETIME*)ts, &stLocal);
// Build a string showing the date and time.
PP("%s: %02d/%02d/%d %02d:%02d %uld", label,
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute,
seconds_until(1, ts));
}
}
// isSPNEGO: true, SPNEGO. false, Kerberos.
static gss_ctx_id_t
new_context(BOOLEAN isSPNEGO)
{
gss_ctx_id_t out = new gss_ctx_id_struct;
if (out == NULL) {
return NULL;
}
out->phCred = NULL;
out->hCtxt.dwLower = out->hCtxt.dwUpper = NULL;
out->established = FALSE;
out->SecPkgContextSizes.cbMaxSignature
= out->SecPkgContextSizes.cbBlockSize
= out->SecPkgContextSizes.cbSecurityTrailer
= 0;
out->nnames.sClientName = out->nnames.sServerName = NULL;
out->isSPNEGO = isSPNEGO;
out->isLocalCred = FALSE;
return out;
}
static gss_cred_id_t
new_cred()
{
gss_cred_id_t out = new gss_cred_id_struct;
out->phCredK = out->phCredS = NULL;
out->time = 0L;
return out;
}
static int
flag_sspi_to_gss(int fin)
{
int fout = 0;
if (fin & ISC_REQ_MUTUAL_AUTH) fout |= GSS_C_MUTUAL_FLAG;
if (fin & ISC_REQ_CONFIDENTIALITY) fout |= GSS_C_CONF_FLAG;
if (fin & ISC_REQ_DELEGATE) fout |= GSS_C_DELEG_FLAG;
if (fin & ISC_REQ_INTEGRITY) fout |= GSS_C_INTEG_FLAG;
if (fin & ISC_REQ_REPLAY_DETECT) fout |= GSS_C_REPLAY_FLAG;
if (fin & ISC_REQ_SEQUENCE_DETECT) fout |= GSS_C_SEQUENCE_FLAG;
return fout;
}
static int
flag_gss_to_sspi(int fin)
{
int fout = 0;
if (fin & GSS_C_MUTUAL_FLAG) fout |= ISC_RET_MUTUAL_AUTH;
if (fin & GSS_C_CONF_FLAG) fout |= ISC_RET_CONFIDENTIALITY;
if (fin & GSS_C_DELEG_FLAG) fout |= ISC_RET_DELEGATE;
if (fin & GSS_C_INTEG_FLAG) fout |= ISC_RET_INTEGRITY;
if (fin & GSS_C_REPLAY_FLAG) fout |= ISC_RET_REPLAY_DETECT;
if (fin & GSS_C_SEQUENCE_FLAG) fout |= ISC_RET_SEQUENCE_DETECT;
return fout;
}
static BOOLEAN
is_same_oid(gss_const_OID o2, gss_const_OID o1)
{
return o1 && o2 && o1->length == o2->length
&& !memcmp(o1->elements, o2->elements, o2->length);
}
static BOOLEAN
has_oid(gss_const_OID_set set, gss_const_OID oid)
{
for (size_t i = 0; i < set->count; i++) {
if (is_same_oid(&set->elements[i], oid)) {
return TRUE;
}
}
return FALSE;
}
static void
show_oid(gss_const_OID mech)
{
if (trace) {
if (is_same_oid(mech, &KRB5_OID)) {
PP("Kerberos mech");
} else if (is_same_oid(mech, &SPNEGO_OID)) {
PP("SPNEGO mech");
} else if (is_same_oid(mech, &USER_NAME_OID)) {
PP("NT_USER_NAME name-type");
} else if (is_same_oid(mech, &KRB5_NAME_OID)) {
PP("KRB5_NAME name-type");
} else if (is_same_oid(mech, &HOST_SERVICE_NAME_OID)) {
PP("NT_HOSTBASED_SERVICE name-type");
} else if (is_same_oid(mech, &EXPORT_NAME_OID)) {
PP("NT_EXPORT_NAME name-type");
} else {
dump("UNKNOWN OID", (PBYTE)mech->elements, mech->length);
}
}
}
static void
show_oid_set(gss_const_OID_set mechs)
{
if (trace) {
if (mechs == NULL) {
PP("OID set is NULL");
return;
}
PP("gss_OID_set.count is %d", (int)mechs->count);
for (size_t i = 0; i < mechs->count; i++) {
show_oid(&mechs->elements[i]);
}
}
}
// Add realm to a name if there was none.
// Returns a newly allocated name.
static WCHAR*
get_full_name(WCHAR* input)
{
// input has realm, no need to add one
for (int i = 0;; i++) {
if (!input[i]) { // the end
break;
}
if (input[i] == L'\\') { // escaped
i++;
continue;
}
if (input[i] == L'@') {
return _wcsdup(input);
}
}
// Always use the default domain
WCHAR* realm = _wgetenv(L"USERDNSDOMAIN");
if (realm == NULL) {
realm = L"";
}
size_t oldlen = wcslen(input);
size_t newlen = oldlen + 1 + wcslen(realm) + 1;
WCHAR* fullname = new WCHAR[newlen];
if (!fullname) {
return NULL;
}
wcscpy_s(fullname, newlen, input);
wcscat_s(fullname, newlen, L"@");
wcscat_s(fullname, newlen, realm);
PP("get_full_name returns %ls", fullname);
return fullname;
}
/* End support section */
/* This section holds GSS-API exported functions */
#define CHECK_OUTPUT(x) if (!x) return GSS_S_CALL_INACCESSIBLE_WRITE;
#define CHECK_BUFFER(b) if (!b || !b->value) return GSS_S_CALL_INACCESSIBLE_READ;
#define CHECK_OID(o) if (!o || !o->elements) return GSS_S_CALL_INACCESSIBLE_READ;
#define CHECK_NAME(n) if (!n || !(n->name)) return GSS_S_BAD_NAME;
#define CHECK_CONTEXT(c) if (!c) return GSS_S_NO_CONTEXT;
#define CHECK_CRED(c) if (!c || (!(cred_handle->phCredK) && !(cred_handle->phCredS))) \
return GSS_S_NO_CRED;
__declspec(dllexport) OM_uint32
gss_release_name(OM_uint32 *minor_status,
gss_name_t *name)
{
PP(">>>> Calling gss_release_name %p...", *name);
if (name != NULL && *name != GSS_C_NO_NAME) {
if ((*name)->name != NULL) {
delete[] (*name)->name;
}
delete *name;
*name = GSS_C_NO_NAME;
}
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_import_name(OM_uint32 *minor_status,
gss_const_buffer_t input_name_buffer,
gss_const_OID input_name_type,
gss_name_t *output_name)
{
PP(">>>> Calling gss_import_name...");
CHECK_BUFFER(input_name_buffer)
CHECK_OUTPUT(output_name)
int len = (int)input_name_buffer->length;
LPSTR input = (LPSTR)input_name_buffer->value;
if (input_name_type != NULL
&& is_same_oid(input_name_type, &EXPORT_NAME_OID)) {
if (len < 4 || input[0] != 4 || input[1] != 1 || input[2] != 0) {
return GSS_S_FAILURE;
}
int mechLen = (int)input[3]; /* including 06 len */
len -= mechLen + 8; /* 4 header bytes, and an int32 length after OID */
if (len <= 0) {
return GSS_S_FAILURE;
}
// Reject if mech is not krb5
if (mechLen - 2!= KRB5_OID.length ||
memcmp(input + 6, KRB5_OID.elements, mechLen - 2)) {
return GSS_S_FAILURE;;
}
input = input + mechLen + 8;
}
SEC_WCHAR* value = new SEC_WCHAR[len + 1];
if (value == NULL) {
goto err;
}
len = MultiByteToWideChar(CP_UTF8, 0, input, len, value, len+1);
if (len == 0) {
goto err;
}
value[len] = 0;
PP("import_name from %ls", value);
if (len > 33 && !wcscmp(value+len-33, L"@WELLKNOWN:ORG.H5L.REFERALS-REALM")) {
// Remove the wellknown referrals realms
value[len-33] = 0;
len -= 33;
} else if (value[len-1] == L'@') {
// Remove the empty realm. It might come from an NT_EXPORT_NAME.
value[len-1] = 0;
len--;
}
if (len == 0) {
goto err;
}
if (input_name_type != NULL
&& is_same_oid(input_name_type, &HOST_SERVICE_NAME_OID)) {
// HOST_SERVICE_NAME_OID takes the form of service@host.
for (int i = 0; i < len; i++) {
if (value[i] == L'\\') {
i++;
continue;
}
if (value[i] == L'@') {
value[i] = L'/';
break;
}
}
PP("Host-based service now %ls", value);
}
PP("import_name to %ls", value);
gss_name_struct* name = new gss_name_struct;
if (name == NULL) {
goto err;
}
name->name = value;
*output_name = (gss_name_t) name;
return GSS_S_COMPLETE;
err:
if (value != NULL) {
delete[] value;
}
return GSS_S_FAILURE;
}
__declspec(dllexport) OM_uint32
gss_compare_name(OM_uint32 *minor_status,
gss_const_name_t name1,
gss_const_name_t name2,
int *name_equal)
{
PP(">>>> Calling gss_compare_name...");
CHECK_NAME(name1)
CHECK_NAME(name2)
CHECK_OUTPUT(name_equal)
*name_equal = 0;
SEC_WCHAR* n1 = name1->name;
SEC_WCHAR* n2 = name2->name;
PP("Comparing %ls and %ls", n1, n2);
int l1 = lstrlen(n1);
int l2 = lstrlen(n2);
int r1 = l1; // position of @ or the end if none
int r2 = l2;
int i;
for (i = 0; i < l1; i++) {
if (n1[i] == L'\\') {
i++;
continue;
}
if (n1[i] == L'@') {
r1 = i;
break;
}
}
for (i = 0; i < l2; i++) {
if (n2[i] == L'\\') {
i++;
continue;
}
if (n2[i] == L'@') {
r2 = i;
break;
}
}
if (l1 < l2 && l1 != r2
|| l2 < l1 && l2 != r1) {
return GSS_S_COMPLETE; // different
}
if (l1 > l2) {
l1 = l2; // choose the smaller one. longer=smaller @ ...
}
// Two names are equal if they are the same or one has no realm and
// one has realm but they have the same name. If both have realm but
// different, they are treated different even if the names are the same.
// Note: the default name concept is not used here.
// Principal names on Windows are case-insensitive, both user name
// and service principal name.
if (CompareStringEx(LOCALE_NAME_SYSTEM_DEFAULT, NORM_IGNORECASE,
n1, l1, n2, l1, NULL, NULL, 0) == CSTR_EQUAL) {
*name_equal = 1;
}
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_canonicalize_name(OM_uint32 *minor_status,
gss_const_name_t input_name,
gss_const_OID mech_type,
gss_name_t *output_name)
{
PP(">>>> Calling gss_canonicalize_name...");
CHECK_NAME(input_name)
CHECK_OID(mech_type)
CHECK_OUTPUT(output_name)
if (!is_same_oid(mech_type, &KRB5_OID)) {
PP("Cannot canonicalize to non-krb5 OID");
return GSS_S_BAD_MECH;
}
gss_name_t names2 = new gss_name_struct;
if (names2 == NULL) {
return GSS_S_FAILURE;
}
names2->name = get_full_name(input_name->name);
if (names2->name == NULL) {
delete names2;
return GSS_S_FAILURE;
}
*output_name = names2;
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_export_name(OM_uint32 *minor_status,
gss_const_name_t input_name,
gss_buffer_t exported_name)
{
PP(">>>> Calling gss_export_name...");
CHECK_NAME(input_name)
CHECK_OUTPUT(exported_name)
OM_uint32 result = GSS_S_FAILURE;
SEC_WCHAR* name = input_name->name;
SEC_WCHAR* fullname = get_full_name(name);
if (!fullname) {
goto err;
}
PP("Make fullname: %ls -> %ls", name, fullname);
int len;
size_t namelen = wcslen(fullname);
if (namelen > 255) {
goto err;
}
len = (int)namelen;
// We only deal with not-so-long names.
// 04 01 00 ** 06 ** OID len:int32 name
int mechLen = KRB5_OID.length;
char* buffer = new char[10 + mechLen + len];
if (buffer == NULL) {
goto err;
}
buffer[0] = 4;
buffer[1] = 1;
buffer[2] = 0;
buffer[3] = 2 + mechLen;
buffer[4] = 6;
buffer[5] = mechLen;
memcpy_s(buffer + 6, mechLen, KRB5_OID.elements, mechLen);
buffer[6 + mechLen] = buffer[7 + mechLen] = buffer[8 + mechLen] = 0;
buffer[9 + mechLen] = (char)len;
len = WideCharToMultiByte(CP_UTF8, 0, fullname, len,
buffer+10+mechLen, len, NULL, NULL);
if (len == 0) {
delete[] buffer;
goto err;
}
exported_name->length = 10 + mechLen + len;
exported_name->value = buffer;
result = GSS_S_COMPLETE;
err:
if (fullname != name) {
delete[] fullname;
}
return result;
}
__declspec(dllexport) OM_uint32
gss_display_name(OM_uint32 *minor_status,
gss_const_name_t input_name,
gss_buffer_t output_name_buffer,
gss_OID *output_name_type)
{
PP(">>>> Calling gss_display_name...");
CHECK_NAME(input_name)
CHECK_OUTPUT(output_name_buffer)
SEC_WCHAR* names = input_name->name;
int len = (int)wcslen(names);
char* buffer = new char[4*len+1];
if (buffer == NULL) {
return GSS_S_FAILURE;
}
len = WideCharToMultiByte(CP_UTF8, 0, names, len, buffer, 4*len, NULL, NULL);
if (len == 0) {
delete[] buffer;
return GSS_S_FAILURE;
}
buffer[len] = 0;
output_name_buffer->length = len;
output_name_buffer->value = buffer;
PP("Name found: %ls -> %d [%s]", names, len, buffer);
if (output_name_type != NULL) {
*output_name_type = &KRB5_NAME_OID;
}
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_acquire_cred(OM_uint32 *minor_status,
gss_const_name_t desired_name,
OM_uint32 time_req,
gss_const_OID_set desired_mechs,
gss_cred_usage_t cred_usage,
gss_cred_id_t *output_cred_handle,
gss_OID_set *actual_mechs,
OM_uint32 *time_rec)
{
PP(">>>> Calling gss_acquire_cred...");
CHECK_OUTPUT(output_cred_handle)
SECURITY_STATUS ss;
TimeStamp ts;
ts.QuadPart = 0;
cred_usage = 0;
PP("AcquireCredentialsHandle with %d %p", cred_usage, desired_mechs);
show_oid_set(desired_mechs);
BOOLEAN reqKerberos, reqSPNEGO;
if (!desired_mechs) {
reqKerberos = reqSPNEGO = TRUE;
} else {
if (has_oid(desired_mechs, &KRB5_OID)) {
PP("reqKerberos");
reqKerberos = TRUE;
}
if (has_oid(desired_mechs, &SPNEGO_OID)) {
PP("reqSPNEGO");
reqSPNEGO = TRUE;
}
if (!reqSPNEGO && !reqKerberos) {
return GSS_S_BAD_MECH;
}
}
if (actual_mechs) {
*actual_mechs = GSS_C_NO_OID_SET;
}
gss_cred_id_t cred = new_cred();
if (cred == NULL) {
goto err;
}
if (reqKerberos) {
cred->phCredK = new CredHandle;
if (cred->phCredK == NULL) {
goto err;
}
ss = AcquireCredentialsHandle(
NULL,
L"Kerberos",
cred_usage == 0 ? SECPKG_CRED_BOTH :
(cred_usage == 1 ? SECPKG_CRED_OUTBOUND : SECPKG_CRED_INBOUND),
NULL,
NULL,
NULL,
NULL,
cred->phCredK,
&ts);
if (!(SEC_SUCCESS(ss))) {
delete cred->phCredK;
cred->phCredK = NULL;
goto err;
}
}
if (reqSPNEGO) {
cred->phCredS = new CredHandle;
if (cred->phCredS == NULL) {
goto err;
}
SEC_WINNT_AUTH_IDENTITY_EX auth;
ZeroMemory(&auth, sizeof(auth));
auth.Version = SEC_WINNT_AUTH_IDENTITY_VERSION;
auth.Length = sizeof(auth);
auth.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
auth.PackageList = (unsigned short*)L"Kerberos";
auth.PackageListLength = 8;
ss = AcquireCredentialsHandle(
NULL,
L"Negotiate",
cred_usage == 0 ? SECPKG_CRED_BOTH :
(cred_usage == 1 ? SECPKG_CRED_OUTBOUND : SECPKG_CRED_INBOUND),
NULL,
&auth,
NULL,
NULL,
cred->phCredS,
&ts);
if (!(SEC_SUCCESS(ss))) {
delete cred->phCredS;
cred->phCredS = NULL;
goto err;
}
}
if (actual_mechs) {
if (gss_create_empty_oid_set(minor_status, actual_mechs)) {
goto err;
}
if (reqKerberos) {
if (gss_add_oid_set_member(minor_status, &KRB5_OID, actual_mechs)) {
goto err;
}
}
if (reqSPNEGO) {
if (gss_add_oid_set_member(minor_status, &SPNEGO_OID, actual_mechs)) {
goto err;
}
}
}
*output_cred_handle = (gss_cred_id_t)cred;
// Note: ts here is weirdly huge, maybe because LSA retains the
// password and can re-acquire a TGT at anytime. It will be
// GSSCredential.INDEFINITE_LIFETIME.
show_time("cred expiration", &ts);
cred->time = seconds_until(1, &ts);
if (time_rec != NULL) {
*time_rec = cred->time;
}
// Since only default cred is supported, if there is a desired_name,
// we must make sure it is the same as the realname of the default cred.
if (desired_name != NULL) {
PP("Acquiring cred with a name. Check if it's me.");
gss_name_t realname;
if (gss_inquire_cred(minor_status, *output_cred_handle, &realname,
NULL, NULL, NULL) != GSS_S_COMPLETE) {
PP("Cannot get owner name of default creds");
goto err;
}
SEC_WCHAR* rnames = realname->name;
SEC_WCHAR* dnames = desired_name->name;
int equals = 0;
gss_compare_name(minor_status, realname, desired_name, &equals);
gss_release_name(minor_status, &realname);
PP("Comparing result: %d", equals);
if (!equals) {
goto err;
}
}
return GSS_S_COMPLETE;
err:
if (cred) {
OM_uint32 dummy;
gss_release_cred(&dummy, &cred);
}
if (actual_mechs) {
OM_uint32 dummy;
gss_release_oid_set(&dummy, actual_mechs);
}
return GSS_S_FAILURE;
}
__declspec(dllexport) OM_uint32
gss_release_cred(OM_uint32 *minor_status,
gss_cred_id_t *cred_handle)
{
PP(">>>> Calling gss_release_cred...");
if (cred_handle && *cred_handle) {
if ((*cred_handle)->phCredK) {
FreeCredentialsHandle((*cred_handle)->phCredK);
delete (*cred_handle)->phCredK;
}
if ((*cred_handle)->phCredS) {
FreeCredentialsHandle((*cred_handle)->phCredS);
delete (*cred_handle)->phCredS;
}
delete *cred_handle;
*cred_handle = GSS_C_NO_CREDENTIAL;
}
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_inquire_cred(OM_uint32 *minor_status,
gss_const_cred_id_t cred_handle,
gss_name_t *name,
OM_uint32 *lifetime,
gss_cred_usage_t *cred_usage,
gss_OID_set *mechanisms)
{
PP(">>>> Calling gss_inquire_cred...");
CHECK_CRED(cred_handle)
CredHandle* cred = cred_handle->phCredK
? cred_handle->phCredK
: cred_handle->phCredS;
SECURITY_STATUS ss;
if (name) {
*name = GSS_C_NO_NAME;
SecPkgCredentials_Names snames;
ss = QueryCredentialsAttributes(cred, SECPKG_CRED_ATTR_NAMES, &snames);
if (!SEC_SUCCESS(ss)) {
return GSS_S_FAILURE;
}
SEC_WCHAR* names = new SEC_WCHAR[lstrlen(snames.sUserName) + 1];
if (names == NULL) {
return GSS_S_FAILURE;
}
StringCchCopy(names, lstrlen(snames.sUserName) + 1, snames.sUserName);
FreeContextBuffer(snames.sUserName);
PP("Allocate new name at %p", names);
gss_name_t name1 = new gss_name_struct;
if (name1 == NULL) {
delete[] names;
return GSS_S_FAILURE;
}
name1->name = names;
*name = (gss_name_t) name1;
}
if (lifetime) {
*lifetime = cred_handle->time;
}
if (cred_usage) {
*cred_usage = 1; // We only support INITIATE_ONLY now
}
if (mechanisms) {
// Useless for Java
}
// Others inquiries not supported yet
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_import_sec_context(OM_uint32 *minor_status,
gss_const_buffer_t interprocess_token,
gss_ctx_id_t *context_handle)
{
// Not transferable, return FAILURE
PP(">>>> Calling UNIMPLEMENTED gss_import_sec_context...");
*minor_status = 0;
return GSS_S_FAILURE;
}
__declspec(dllexport) OM_uint32
gss_init_sec_context(OM_uint32 *minor_status,
gss_const_cred_id_t initiator_cred_handle,
gss_ctx_id_t *context_handle,
gss_const_name_t target_name,
gss_const_OID mech_type,
OM_uint32 req_flags,
OM_uint32 time_req,
gss_const_channel_bindings_t input_chan_bindings,
gss_const_buffer_t input_token,
gss_OID *actual_mech_type,
gss_buffer_t output_token,
OM_uint32 *ret_flags,
OM_uint32 *time_rec)
{
PP(">>>> Calling gss_init_sec_context...");
CHECK_NAME(target_name)
CHECK_OUTPUT(output_token)
SECURITY_STATUS ss;
TimeStamp lifeTime;
SecBufferDesc inBuffDesc;
SecBuffer inSecBuff;
SecBufferDesc outBuffDesc;
SecBuffer outSecBuff;
BOOLEAN isSPNEGO = is_same_oid(mech_type, &SPNEGO_OID);
gss_ctx_id_t pc;
output_token->length = 0;
output_token->value = NULL;
BOOLEAN firstTime = (*context_handle == GSS_C_NO_CONTEXT);
PP("First time? %d", firstTime);
if (firstTime) {
pc = new_context(isSPNEGO);
if (pc == NULL) {
return GSS_S_FAILURE;
}
*context_handle = (gss_ctx_id_t) pc;
} else {
pc = *context_handle;
}
if (pc == NULL) {
return GSS_S_NO_CONTEXT;
}
DWORD outFlag;
TCHAR outName[100];
OM_uint32 minor;
gss_buffer_desc tn;
gss_display_name(&minor, target_name, &tn, NULL);
int len = MultiByteToWideChar(CP_UTF8, 0, (LPCCH)tn.value, (int)tn.length,
outName, (sizeof(outName) / sizeof(outName[0])) - 1);
if (len == 0) {
goto err;
}
outName[len] = 0;
int flag = flag_gss_to_sspi(req_flags) | ISC_REQ_ALLOCATE_MEMORY;
outBuffDesc.ulVersion = SECBUFFER_VERSION;
outBuffDesc.cBuffers = 1;
outBuffDesc.pBuffers = &outSecBuff;
outSecBuff.BufferType = SECBUFFER_TOKEN;
if (!firstTime) {
inBuffDesc.ulVersion = SECBUFFER_VERSION;
inBuffDesc.cBuffers = 1;
inBuffDesc.pBuffers = &inSecBuff;
inSecBuff.BufferType = SECBUFFER_TOKEN;
inSecBuff.cbBuffer = (ULONG)input_token->length;
inSecBuff.pvBuffer = input_token->value;
} else if (!pc->phCred) {
if (isSPNEGO && initiator_cred_handle
&& initiator_cred_handle->phCredS) {
PP("Find SPNEGO credentials");
pc->phCred = initiator_cred_handle->phCredS;
pc->isLocalCred = FALSE;
} else if (!isSPNEGO && initiator_cred_handle
&& initiator_cred_handle->phCredK) {
PP("Find Kerberos credentials");
pc->phCred = initiator_cred_handle->phCredK;
pc->isLocalCred = FALSE;
} else {
PP("No credentials provided, acquire myself");
CredHandle* newCred = new CredHandle;
SEC_WINNT_AUTH_IDENTITY_EX auth;
ZeroMemory(&auth, sizeof(auth));
auth.Version = SEC_WINNT_AUTH_IDENTITY_VERSION;
auth.Length = sizeof(auth);
auth.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
auth.PackageList = (unsigned short*)L"Kerberos";
auth.PackageListLength = 8;
ss = AcquireCredentialsHandle(
NULL,
isSPNEGO ? L"Negotiate" : L"Kerberos",
SECPKG_CRED_OUTBOUND,
NULL,
isSPNEGO ? &auth : NULL,
NULL,
NULL,
newCred,
&lifeTime);
if (!(SEC_SUCCESS(ss))) {
delete newCred;
goto err;
}
pc->phCred = newCred;
pc->isLocalCred = TRUE;
}
}
ss = InitializeSecurityContext(
pc->phCred,
firstTime ? NULL : &pc->hCtxt,
outName,
flag,
0,
SECURITY_NATIVE_DREP,
firstTime ? NULL : &inBuffDesc,
0,
&pc->hCtxt,
&outBuffDesc,
&outFlag,
&lifeTime);
if (!SEC_SUCCESS(ss)) {
PP("InitializeSecurityContext failed");
goto err;
}
pc->flags = *ret_flags = flag_sspi_to_gss(outFlag);
// Ignore the result of the next call. Might fail before context established.
QueryContextAttributes(
&pc->hCtxt, SECPKG_ATTR_SIZES, &pc->SecPkgContextSizes);
PP("cbMaxSignature: %ld. cbBlockSize: %ld. cbSecurityTrailer: %ld",
pc->SecPkgContextSizes.cbMaxSignature,
pc->SecPkgContextSizes.cbBlockSize,
pc->SecPkgContextSizes.cbSecurityTrailer);
output_token->length = outSecBuff.cbBuffer;
if (outSecBuff.cbBuffer) {
// No idea how user would free the data. Let's duplicate one.
output_token->value = new char[outSecBuff.cbBuffer];
if (!output_token->value) {
FreeContextBuffer(outSecBuff.pvBuffer);
output_token->length = 0;
goto err;
}
memcpy(output_token->value, outSecBuff.pvBuffer, outSecBuff.cbBuffer);
FreeContextBuffer(outSecBuff.pvBuffer);
}
if (ss == SEC_I_CONTINUE_NEEDED) {
return GSS_S_CONTINUE_NEEDED;