-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspnego.c
162 lines (126 loc) · 3.99 KB
/
spnego.c
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
#include <utils.h>
#ifndef WIN32_WINNT
# define WIN32_WINNT 0x500
#endif
#define SECURITY_WIN32
#include <security.h>
#ifndef __GNUC__
#pragma comment(lib, "secur32.lib")
#endif
#define SPN_MAX_LENGTH 260
#define SPN_PREFIX "HTTP/"
#define ISC_FLAGS ISC_REQ_ALLOCATE_MEMORY
int spnego_request(const char *host, char **out_buf, void **out_creds, void **out_ctxt) {
PCredHandle phCred;
PCtxtHandle phCtxt;
SecBufferDesc outputBuffers;
PSecBuffer pBuffer;
ULONG contextAttr;
TimeStamp exp;
SECURITY_STATUS ret = SEC_E_OK;
char spn[SPN_MAX_LENGTH];
int i;
char *_buf = NULL;
int _len = 0;
snprintf(spn, SPN_MAX_LENGTH, "%s%s", SPN_PREFIX, host);
phCred = (PCredHandle) new(sizeof(CredHandle));
phCtxt = (PCtxtHandle) new(sizeof(CtxtHandle));
ret = AcquireCredentialsHandle(NULL, "Negotiate", SECPKG_CRED_OUTBOUND,
NULL, NULL, NULL, NULL, phCred, &exp);
if (FAILED(ret)) {
free(phCtxt);
free(phCred);
return 0;
}
outputBuffers.ulVersion = SECBUFFER_VERSION;
outputBuffers.cBuffers = 0;
outputBuffers.pBuffers = NULL;
ret = InitializeSecurityContext(phCred, NULL, spn, ISC_FLAGS, 0,
SECURITY_NATIVE_DREP, NULL, 0, phCtxt, &outputBuffers, &contextAttr,
&exp);
if (FAILED(ret)) {
FreeCredentialsHandle(phCred);
free(phCred);
free(phCtxt);
return 0;
}
if (ret != SEC_I_CONTINUE_NEEDED) {
for (i=0; i<outputBuffers.cBuffers; i++)
FreeContextBuffer(outputBuffers.pBuffers[i]);
DeleteSecurityContext(phCtxt);
FreeCredentialsHandle(phCred);
free(phCred);
free(phCtxt);
return 0;
}
pBuffer = outputBuffers[0].pBuffers;
_len = pBuffer->cbBuffer;
_buf = new(_len + 1);
memcpy(_buf, pBuffer->pvBuffer, _len);
for (i=0; i<outputBuffers.cBuffers; i++)
FreeContextBuffer(outputBuffers.pBuffers[i].pvBuffer);
*out_buf = _buf;
*out_creds = phCred;
*out_ctxt = phCtxt;
return _len;
}
int spnego_response(const char *host, const char *resp, const int resp_len, const void *creds,
const void *ctxt, char **out_buf) {
PCredHandle phCred;
PCtxtHandle phCtxt;
SecBufferDesc inputBuffers;
SecBuffer inputBuffer;
SecBufferDesc outputBuffers;
PSecBuffer pBuffer;
ULONG contextAttr;
TimeStamp exp;
SECURITY_STATUS ret = SEC_E_OK;
char spn[SPN_MAX_LENGTH];
int i;
char *_buf = NULL;
int _len = 0;
snprintf(spn, SPN_MAX_LENGTH, "%s%s", SPN_PREFIX, host);
phCred = (PCredHandle) creds;
phCtxt = (PCtxtHandle) ctxt;
inputBuffers.cBuffers = 1;
inputBuffers.ulVersion = SECBUFFER_VERSION;
inputBuffers.pBuffers = &inputBuffer;
inputBuffer.cbBuffer = resp_len;
inputBuffer.BufferType = SECBUFFER_TOKEN;
inputBuffer.pvBuffer = resp;
outputBuffers.cBuffers = 0;
outputBuffers.ulVersion = SECBUFFER_VERSION;
outputBuffers.pBuffers = NULL;
ret = InitializeSecurityContext(phCred, phCtxt, spn, ISC_FLAGS, 0,
SECURITY_NATIVE_DREP, &inputBuffers, 0, phCtxt, &outputBuffers, &contextAttr,
&exp);
if (FAILED(ret))
return -1;
if (ret != SEC_E_OK) {
for (i=0; i < outputBuffers.cBuffers; i++)
FreeContextBuffer(outputBuffers.pBuffers[i].pvBuffer)
return -1;
}
if (outputBuffers.cBuffers > 0) {
pBuffer = outputBuffers[0].pBuffers;
_len = pBuffer->cbBuffer;
_buf = new(_len + 1);
memcpy(_buf, pBuffer->pvBuffer, _len);
for (i=0; i<outputBuffers.cBuffers; i++)
FreeContextBuffer(outputBuffers.pBuffers[i].pvBuffer);
*out_buf = _buf;
}
return _len;
}
void spnego_free(void **creds, void **ctxt) {
if (*creds) {
FreeCredentialsHandle((PCredHandle) *creds);
free(*creds);
*creds = NULL;
}
if (*ctxt) {
DeleteSecurityContext((PCtxtHandle) *ctxt);
free(*ctxt);
*ctxt = NULL;
}
}