-
Notifications
You must be signed in to change notification settings - Fork 0
/
sspi.cpp
1524 lines (1374 loc) · 44.4 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) 2018, 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 only speaks krb5. SPNEGO is supported by creating its own NetTokenInit
// and parsing incoming NegTokenResp tokens. This ensures no other mechanism
// (Ex: NTLM) is chosen.
//
// 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>
#define GSS_DLL_FILE
#include <gssapi.h>
#define SECURITY_WIN32
#include <sspi.h>
#pragma comment(lib, "secur32.lib")
#define PP(fmt, ...) \
if (trace) { \
fprintf(stdout, "SSPI (%ld): ", __LINE__); \
fprintf(stdout, fmt, ##__VA_ARGS__); \
fprintf(stdout, "\n"); \
fflush(stdout); \
}
#define SEC_SUCCESS(Status) (*minor_status = Status, (Status) >= 0)
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
// When KRB5_TRACE is set, debug info goes to stdout. The value is ignored.
char* trace = getenv("KRB5_TRACE");
void
dump(LPSTR title, PBYTE data, DWORD len)
{
if (trace) {
printf("==== %s ====\n", title);
for (DWORD i = 0; i < len; i++) {
if (i != 0 && i % 16 == 0) {
printf("\n");
}
printf("%02X ", *(data + i) & 0xff);
}
printf("\n");
}
}
gss_OID_desc KRB5_OID = {9, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"};
gss_OID_desc SPNEGO_OID = {6, "\x2b\x06\x01\x05\x05\x02"};
gss_OID_desc USER_NAME_OID = {10, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x01"};
gss_OID_desc HOST_SERVICE_NAME_OID = {10, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
gss_OID_desc EXPORT_NAME_OID = {6, "\x2b\x06\x01\x05\x06\x04"};
// gss_name_t is Name*
typedef struct {
SEC_WCHAR* name;
} Name;
// gss_ctx_id_t is Context*
typedef struct {
CredHandle* phCred;
CtxtHandle hCtxt;
DWORD cbMaxMessage;
SecPkgContext_Sizes SecPkgContextSizes;
SecPkgContext_NativeNames nnames;
BOOLEAN established;
} Context;
// gss_cred_id_t is Credential*
typedef struct {
CredHandle* phCred;
long time;
} Credential;
/* This section holds supporting functions that are not exported */
// Prepend ASN.1 tag+length to data.
//
// [in, out] data: the data, which already had enough allocated space before
// its head when called. Head moved backwards after called.
// [in, out] pLen: a counter to maintain
// [in] len: the ASN.1 length (MUST be less than 2^16)
// [in] tag: the ASN.1 tag
void
add_der_head(char** data, int* pLen, int len, int tag)
{
char* pos = *data;
int lenlen;
if (len <= 0x7f) {
pos[-1] = (char)len;
lenlen = 1;
} else if (len < 256) {
pos[-1] = (char)len;
pos[-2] = (char)0x81;
lenlen = 2;
} else {
pos[-1] = (char)(len & 0xff);
pos[-2] = (char)(len >> 8);
pos[-3] = (char)0x82;
lenlen = 3;
}
pos[-1-lenlen] = (char)tag;
*pLen += 1 + lenlen;
*data = pos - 1 - lenlen;
}
// Prepend bytes to data.
//
// [in, out] data: the data, which already had enough allocated space before
// its head when called. Head moved backwards after called.
// [in, out] pLen: a counter to maintain
// [in] in: a block of bytes
// [in] len: length of in
void
add_data(char** data, int* pLen, char* in, int len)
{
char* pos = *data;
memcpy_s(pos - len, len, in, len);
*pLen += len;
*data = pos - len;
}
// Wrap a krb5 token into NegTokenInit
// Returns 1 for success, 0 for failure
int
krb5_to_spnego(gss_buffer_t input, gss_buffer_t output)
{
int len = (int)input->length;
if (len > 60000) { // Too long, need 3 bytes DER length
return 0;
}
// Just enough for a token with 2-bytes length
int maxLen = len + 43;
char* buffer = new char[maxLen];
memcpy_s(buffer + maxLen - len, len, input->value, len);
char* pos = buffer + maxLen - len;
// mechToken [2] OCTET STRING OPTIONAL,
add_der_head(&pos, &len, len, 4);
add_der_head(&pos, &len, len, 0xA2);
// mechTypes [0] MechTypeList,
add_data(&pos, &len, (char*)KRB5_OID.elements, KRB5_OID.length);
add_der_head(&pos, &len, KRB5_OID.length, 6);
add_der_head(&pos, &len, KRB5_OID.length + 2, 0x30);
add_der_head(&pos, &len, KRB5_OID.length + 4, 0xA0);
// NegTokenInit ::= SEQUENCE { ... }
add_der_head(&pos, &len, len, 0x30);
// negTokenInit [0] NegTokenInit,
add_der_head(&pos, &len, len, 0xA0);
// [APPLICATION 0] IMPLICIT SEQUENCE {
// thisMech MechType,
// innerContextToken ANY DEFINED BY thisMech
add_data(&pos, &len, (char*)SPNEGO_OID.elements, SPNEGO_OID.length);
add_der_head(&pos, &len, SPNEGO_OID.length, 6);
add_der_head(&pos, &len, len, 0x60);
if (len != maxLen) {
char* result = new char[len];
memcpy_s(result, len, buffer + maxLen - len, len);
delete[] buffer;
buffer = result;
}
output->value = buffer;
output->length = len;
return 1;
}
// Advances pass ASN.1 tag and length.
// Returns the length value
int
skip_tag_len(char** data)
{
char* pos = *data;
pos++;
int n = *pos & 0xff;
if (n < 128) {
pos++;
} else if (n == 0x81) {
pos++;
n = *pos & 0xff;
pos++;
} else if (n == 0x82) {
pos++;
n = *pos & 0xff;
pos++;
n = (n << 8) | (*pos & 0xff);
pos++;
} else {
return -1;
}
*data = pos;
return n;
}
// Extract a krb5 token from a NegTokenResp. Returns the negState,
// or -1 if there is no negState.
int
spnego_to_krb5(gss_buffer_t input, gss_buffer_t output)
{
int result = -1;
char* data = (char*)input->value;
skip_tag_len(&data);
skip_tag_len(&data);
while (data - (char*)input->value < (int)input->length) {
if (*data == (char)0xA2) {
skip_tag_len(&data);
output->length = skip_tag_len(&data);
output->value = data;
if (input->length - ((char*)output->value - (char*)input->value)
< output->length) {
// inner token has a length field longer than outer token
break;
}
return result;
} else {
if (*data == (char)0xA0) {
result = data[4];
}
int tagAndLen = skip_tag_len(&data);
data += tagAndLen;
}
}
output->length = 0;
output->value = NULL;
return result;
}
void
showTime(TimeStamp* ts)
{
if (trace) {
SYSTEMTIME stLocal;
FileTimeToSystemTime((FILETIME*)ts, &stLocal);
// Build a string showing the date and time.
PP("---------------");
PP("TS low high %ld %ld", ts->LowPart, ts->HighPart);
PP("Local: %02d/%02d/%d %02d:%02d",
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);
}
}
long
SecondsUntil(TimeStamp *time)
{
// time is local time
ULARGE_INTEGER uiLocal;
FILETIME nowUTC, nowLocal;
GetSystemTimeAsFileTime(&nowUTC);
if (FileTimeToLocalFileTime(&nowUTC, &nowLocal) == 0) {
return -1;
}
uiLocal.HighPart = nowLocal.dwHighDateTime;
uiLocal.LowPart = nowLocal.dwLowDateTime;
long diff = (long)((time->QuadPart - uiLocal.QuadPart) / 10000000);
if (diff < 0 || diff > 8640000) {
// TODO: AcquireCredentialsHandle returns a strange TimeStamp.
PP("SecondsUntil is %ld. Change to 1 day", diff);
diff = 86400;
}
return diff;
}
Context*
NewContext(OM_uint32 *minor_status)
{
SECURITY_STATUS ss;
PSecPkgInfo pkgInfo;
Context* out = new Context;
if (out == NULL) {
return NULL;
}
ss = QuerySecurityPackageInfo(L"Kerberos", &pkgInfo);
if (!SEC_SUCCESS(ss)) {
delete out;
return NULL;
}
out->phCred = NULL;
out->cbMaxMessage = pkgInfo->cbMaxToken;
FreeContextBuffer(pkgInfo);
return out;
}
int
flagSspi2Gss(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;
}
int
flagGss2Sspi(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;
}
BOOLEAN
isSameOID(gss_OID o2, gss_OID o1)
{
return o1->length == o2->length
&& !memcmp(o1->elements, o2->elements, o2->length);
}
void
displayOID(gss_OID mech)
{
if (trace) {
if (isSameOID(mech, &KRB5_OID)) {
PP("Kerberos OID");
} else if (isSameOID(mech, &SPNEGO_OID)) {
PP("SPNEGO OID");
} else {
dump("UNKNOWN OID", (PBYTE)mech->elements, mech->length);
}
}
}
void
displayOidSet(gss_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 (int i = 0; i < mechs->count; i++) {
displayOID(&mechs->elements[i]);
}
}
}
// Add realm to a name if there was none.
// Returns a newly allocated name.
WCHAR*
get_full_name(WCHAR* input)
{
// input has realm, no need to add one
if (wcschr(input, '@')) {
return input;
}
// Is this a host-based service name? Ex: service/host.domain.com.
// Then we assume the realm is DOMAIN.COM.
WCHAR* realm = wcsrchr(input, '/'); // /host.domain.com
if (realm) {
realm = wcschr(realm, '.'); // .domain.com
if (realm != NULL) {
realm++; // domain.com
}
}
// Otherwise we use the default domain
if (realm == NULL) {
realm = _wgetenv(L"USERDNSDOMAIN");
if (realm == NULL) {
realm = L"";
}
}
size_t oldlen = wcslen(input);
WCHAR* fullname = new WCHAR[oldlen + 1 + wcslen(realm) + 1];
if (!fullname) {
return NULL;
}
wcscpy_s(fullname, oldlen + 1, input);
fullname[oldlen] = '@';
wcscpy_s(fullname + oldlen + 1, wcslen(realm) + 1, realm);
_wcsupr(fullname + oldlen + 1);
return fullname;
}
/* End support section */
/* This section holds GSS-API exported functions */
#define CHECK_OUTPUT(x) if (!x) return GSS_S_FAILURE;
#define CHECK_BUFFER(b) if (!b || !b->value) return GSS_S_FAILURE;
#define CHECK_OID(o) if (!o || !o->elements) return GSS_S_FAILURE;
#define CHECK_NAME(n) if (!n || !(((Name*)n)->name)) return GSS_S_BAD_NAME;
#define CHECK_CONTEXT(c) if (!c) return GSS_S_NO_CONTEXT;
#define CHECK_CRED(c) if (!c || !(((Credential*)cred_handle)->phCred)) \
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) {
Name* name1 = (Name*)*name;
if (name1->name != NULL) {
delete[] name1->name;
}
delete name1;
*name = GSS_C_NO_NAME;
}
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_import_name(OM_uint32 *minor_status,
gss_buffer_t input_name_buffer,
gss_OID input_name_type,
gss_name_t *output_name)
{
PP(">>>> Calling gss_import_name...");
CHECK_BUFFER(input_name_buffer)
CHECK_OID(input_name_type)
CHECK_OUTPUT(output_name)
int len = (int)input_name_buffer->length;
LPSTR input = (LPSTR)input_name_buffer->value;
BOOLEAN isNegotiate = true;
if (isSameOID(input_name_type, &EXPORT_NAME_OID)) {
// Skip OID and other headers
len -= (int)input[3] + 8;
isNegotiate = (int)input[3] == 6;
input = input + (int)input[3] + 8;
}
SEC_WCHAR* value = new SEC_WCHAR[len + 1];
if (value == NULL) {
goto err;
}
len = MultiByteToWideChar(CP_ACP, 0, input, len, value, len+1);
if (len == 0) {
goto err;
}
value[len] = 0;
if (input_name_type != NULL
&& isSameOID(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] == '@') {
value[i] = '/';
break;
}
}
}
Name* name = new Name;
if (name == NULL) {
goto err;
}
name->name = value;
*output_name = (gss_name_t) name;
return GSS_S_COMPLETE;
err:
if (value != NULL) {
delete[] value;
}
if (name != NULL) {
delete name;
}
return GSS_S_FAILURE;
}
__declspec(dllexport) OM_uint32
gss_compare_name(OM_uint32 *minor_status,
gss_name_t name1,
gss_name_t name2,
int *name_equal)
{
PP(">>>> Calling gss_compare_name...");
CHECK_NAME(name1)
CHECK_NAME(name2)
CHECK_OUTPUT(name_equal)
SEC_WCHAR* names1 = ((Name*)name1)->name;
SEC_WCHAR* names2 = ((Name*)name2)->name;
if (lstrcmp(names1, names2)) {
*name_equal = 0;
} else {
*name_equal = 1;
}
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_canonicalize_name(OM_uint32 *minor_status,
gss_name_t input_name,
gss_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)
Name* names1 = (Name*)input_name;
Name* names2 = new Name;
if (names2 == NULL) {
return GSS_S_FAILURE;
}
names2->name = new SEC_WCHAR[lstrlen(names1->name) + 1];
if (names2->name == NULL) {
delete names2;
return GSS_S_FAILURE;
}
StringCchCopy(names2->name, lstrlen(names1->name) + 1, names1->name);
*output_name = (gss_name_t)names2;
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_export_name(OM_uint32 *minor_status,
gss_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 = ((Name*)input_name)->name;
SEC_WCHAR* fullname = get_full_name(name);
if (!fullname) {
goto err;
}
PP("%ls -> %ls", name, fullname);
int len = (int)wcslen(fullname);
if (len < 256) {
// 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_ACP, 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_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 = ((Name*)input_name)->name;
int len = (int)wcslen(names);
char* buffer = new char[len+1];
if (buffer == NULL) {
return GSS_S_FAILURE;
}
len = WideCharToMultiByte(CP_ACP, 0, names, len, buffer, len, NULL, NULL);
if (len == 0) {
return GSS_S_FAILURE;
}
buffer[len] = 0;
output_name_buffer->length = len;
output_name_buffer->value = buffer;
PP("Name found: %ls", names);
PP("%d [%s]", len, buffer);
if (output_name_type != NULL) {
*output_name_type = &USER_NAME_OID;
}
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_acquire_cred(OM_uint32 *minor_status,
gss_name_t desired_name,
OM_uint32 time_req,
gss_OID_set desired_mech,
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_mech);
displayOidSet(desired_mech);
Credential* cred = new Credential;
cred->phCred = new CredHandle;
if (cred == NULL || cred->phCred == NULL) {
return GSS_S_FAILURE;
}
ts.QuadPart = 0;
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->phCred,
&ts);
actual_mechs = &desired_mech; // TODO: dup?
*output_cred_handle = (void*)cred;
showTime(&ts);
cred->time = SecondsUntil(&ts);
if (time_rec != NULL) {
*time_rec = cred->time;
}
if (desired_name != NULL) {
gss_name_t realname;
if (gss_inquire_cred(minor_status, *output_cred_handle, &realname,
NULL, NULL, NULL) != GSS_S_COMPLETE) {
return GSS_S_FAILURE;
}
SEC_WCHAR* dnames = ((Name*)desired_name)->name;
SEC_WCHAR* rnames = ((Name*)realname)->name;
PP("comp name %ls %ls", dnames, rnames);
int cmp = lstrcmp(dnames, rnames);
gss_release_name(minor_status, &realname);
return cmp ? GSS_S_FAILURE : GSS_S_COMPLETE; // Only support default cred
}
return GSS_S_COMPLETE;
}
__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) {
Credential* cred = (Credential*)*cred_handle;
FreeCredentialsHandle(cred->phCred);
delete cred->phCred;
delete cred;
*cred_handle = GSS_C_NO_CREDENTIAL;
}
return GSS_S_COMPLETE;
}
__declspec(dllexport) OM_uint32
gss_inquire_cred(OM_uint32 *minor_status,
gss_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 = ((Credential*)cred_handle)->phCred;
SECURITY_STATUS ss;
if (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("new name at %p", names);
Name* name1 = new Name;
if (name1 == NULL) {
delete[] names;
return GSS_S_FAILURE;
}
name1->name = names;
*name = (gss_name_t) name1;
}
if (lifetime) {
*lifetime = ((Credential*)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_buffer_t interprocess_token,
gss_ctx_id_t *context_handle)
{
// Not transferable, return FAILURE
PP(">>>> Calling UNIMPLEMENTED gss_import_sec_context...");
return GSS_S_FAILURE;
}
__declspec(dllexport) OM_uint32
gss_init_sec_context(OM_uint32 *minor_status,
gss_cred_id_t initiator_cred_handle,
gss_ctx_id_t *context_handle,
gss_name_t target_name,
gss_OID mech_type,
OM_uint32 req_flags,
OM_uint32 time_req,
gss_channel_bindings_t input_chan_bindings,
gss_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;
Context* pc;
if (input_token->length == 0) {
pc = NewContext(minor_status);
if (pc == NULL) {
return GSS_S_FAILURE;
}
Credential* cred = (Credential*)initiator_cred_handle;
if (cred != NULL) {
pc->phCred = cred->phCred;
}
*context_handle = (gss_ctx_id_t) pc;
} else {
pc = (Context*)*context_handle;
}
if (pc == NULL) {
return GSS_S_NO_CONTEXT;
}
output_token->length = pc->cbMaxMessage;
output_token->value = new char[pc->cbMaxMessage];
if (output_token->value == NULL) {
return GSS_S_FAILURE;
}
DWORD outFlag;
TCHAR outName[100];
OM_uint32 minor;
gss_buffer_desc tn;
gss_display_name(&minor, target_name, &tn, NULL);
int len = MultiByteToWideChar(CP_ACP, 0, (LPCCH)tn.value, (int)tn.length,
outName, (int)tn.length);
if (len == 0) {
return GSS_S_FAILURE;
}
outName[len] = 0;
BOOL pfDone;
int flag = flagGss2Sspi(req_flags);
OutBuffDesc.ulVersion = SECBUFFER_VERSION;
OutBuffDesc.cBuffers = 1;
OutBuffDesc.pBuffers = &OutSecBuff;
OutSecBuff.cbBuffer = (ULONG)output_token->length;
OutSecBuff.BufferType = SECBUFFER_TOKEN;
OutSecBuff.pvBuffer = output_token->value;
if (input_token->value) {
gss_buffer_desc newBuffer;
if (isSameOID(mech_type, &SPNEGO_OID)) {
PP("Extract Kerberos token from Negotiate");
int negState = spnego_to_krb5(input_token, &newBuffer);
PP("negState is %d", negState);
if (newBuffer.length == 0) {
output_token->length = 0;
output_token->value = NULL;
return negState == 0 ? GSS_S_COMPLETE : GSS_S_FAILURE;
}
if (negState == 2 || negState == 3) {
return GSS_S_FAILURE;
}
PP("Extract complete. %d", (int)newBuffer.length);
} else {
newBuffer.length = input_token->length;
newBuffer.value = input_token->value;
}
InBuffDesc.ulVersion = SECBUFFER_VERSION;
InBuffDesc.cBuffers = 1;
InBuffDesc.pBuffers = &InSecBuff;
InSecBuff.BufferType = SECBUFFER_TOKEN;
InSecBuff.cbBuffer = (ULONG)newBuffer.length;
InSecBuff.pvBuffer = newBuffer.value;
} else {
if (!pc->phCred) {
PP("No credentials provided, acquire automatically");
CredHandle* newCred = new CredHandle;
ss = AcquireCredentialsHandle(
NULL,
L"Kerberos",
SECPKG_CRED_OUTBOUND,
NULL,
NULL,
NULL,
NULL,
newCred,
&Lifetime);
pc->phCred = newCred;
PP("end");
if (!(SEC_SUCCESS(ss))) {
PP("Failed");
return GSS_S_FAILURE;
}
} else {
PP("Credentials OK");
}
}
ss = InitializeSecurityContext(
pc->phCred,
input_token->value ? &pc->hCtxt : NULL,
outName,
flag,
0,
SECURITY_NATIVE_DREP,
input_token->value ? &InBuffDesc : NULL,
0,
&pc->hCtxt,
&OutBuffDesc,
&outFlag,
&Lifetime);
if (!SEC_SUCCESS(ss)) {
return GSS_S_FAILURE;
}
if ((SEC_I_COMPLETE_NEEDED == ss)
|| (SEC_I_COMPLETE_AND_CONTINUE == ss)) {
ss = CompleteAuthToken(&pc->hCtxt, &OutBuffDesc);
if (!SEC_SUCCESS(ss)) {
return GSS_S_FAILURE;
}
}
output_token->length = OutSecBuff.cbBuffer;
if (output_token->length > 0 && isSameOID(mech_type, &SPNEGO_OID)) {
PP("Wrap Kerberos token in Negotiate");
gss_buffer_desc newBuffer;
if (krb5_to_spnego(output_token, &newBuffer) == 0) {
return GSS_S_CONTINUE_NEEDED;
}
output_token->length = newBuffer.length;
output_token->value = newBuffer.value;
}
pfDone = !((SEC_I_CONTINUE_NEEDED == ss) ||
(SEC_I_COMPLETE_AND_CONTINUE == ss));
outFlag = flagSspi2Gss(outFlag);
PP("Done? %d outFlag: %d", pfDone, outFlag);
*ret_flags = (OM_uint32)outFlag;
if (ss == SEC_I_CONTINUE_NEEDED) {
return GSS_S_CONTINUE_NEEDED;
} else {
ss = QueryContextAttributes(
&pc->hCtxt, SECPKG_ATTR_SIZES, &pc->SecPkgContextSizes);
if (!SEC_SUCCESS(ss)) {
return GSS_S_FAILURE;
}
pc->established = true;
ss = QueryContextAttributes(&pc->hCtxt, SECPKG_ATTR_NATIVE_NAMES, &pc->nnames);
PP("Names. %ls %ls", pc->nnames.sClientName, pc->nnames.sServerName);
if (!SEC_SUCCESS(ss)) {
return GSS_S_FAILURE;
}
// SPENGO needs another round even if Kerberos is done
if (isSameOID(mech_type, &SPNEGO_OID) && input_token->length == 0) {
return GSS_S_CONTINUE_NEEDED;
} else {
*ret_flags |= GSS_C_PROT_READY_FLAG;
return GSS_S_COMPLETE;
}
}
}
__declspec(dllexport) OM_uint32
gss_accept_sec_context(OM_uint32 *minor_status,
gss_ctx_id_t *context_handle,
gss_cred_id_t acceptor_cred_handle,
gss_buffer_t input_token,
gss_channel_bindings_t input_chan_bindings,
gss_name_t *src_name,
gss_OID *mech_type,
gss_buffer_t output_token,
OM_uint32 *ret_flags,
OM_uint32 *time_rec,
gss_cred_id_t *delegated_cred_handle)
{
PP(">>>> Calling UNIMPLEMENTED gss_accept_sec_context...");
return GSS_S_FAILURE;
}
__declspec(dllexport) OM_uint32
gss_inquire_context(OM_uint32 *minor_status,
gss_ctx_id_t context_handle,
gss_name_t *src_name,
gss_name_t *targ_name,
OM_uint32 *lifetime_rec,
gss_OID *mech_type,
OM_uint32 *ctx_flags,
int *locally_initiated,
int *open)
{
PP(">>>> Calling gss_inquire_context...");
CHECK_CONTEXT(context_handle)
Context* pc = (Context*) context_handle;
Name* n1 = NULL;
Name* n2 = NULL;