/
sspi.c
430 lines (358 loc) · 12.3 KB
1
2
3
/*
* OpenConnect (SSL + DTLS) VPN client
*
4
* Copyright © 2008-2015 Intel Corporation.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
*
* Author: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include <config.h>
#include <errno.h>
#include <string.h>
#include "openconnect-internal.h"
26
static int sspi_setup(struct openconnect_info *vpninfo, struct http_auth_state *auth_state, const char *service, int proxy)
27
28
{
SECURITY_STATUS status;
29
struct oc_text_buf *buf = buf_alloc();
30
31
32
buf_append_utf16le(buf, service);
buf_append_utf16le(buf, "/");
33
buf_append_utf16le(buf, proxy ? vpninfo->proxy : vpninfo->hostname);
34
35
36
37
if (buf_error(buf))
return buf_free(buf);
38
auth_state->sspi_target_name = (wchar_t *)buf->data;
39
40
buf->data = NULL;
buf_free(buf);
41
42
43
status = AcquireCredentialsHandleW(NULL, (SEC_WCHAR *)L"Negotiate",
SECPKG_CRED_OUTBOUND, NULL, NULL,
44
NULL, NULL, &auth_state->sspi_cred,
45
NULL);
46
47
48
if (status != SEC_E_OK) {
vpn_progress(vpninfo, PRG_ERR,
_("AcquireCredentialsHandle() failed: %lx\n"), status);
49
50
free(auth_state->sspi_target_name);
auth_state->sspi_target_name = NULL;
51
52
53
54
55
56
return -EIO;
}
return 0;
}
57
58
int gssapi_authorization(struct openconnect_info *vpninfo, int proxy,
struct http_auth_state *auth_state, struct oc_text_buf *hdrbuf)
59
60
61
62
63
64
65
{
SECURITY_STATUS status;
SecBufferDesc input_desc, output_desc;
SecBuffer in_token, out_token;
ULONG ret_flags;
int first = 1;
66
if (auth_state->state == AUTH_AVAILABLE && sspi_setup(vpninfo, auth_state, "HTTP", proxy)) {
67
auth_state->state = AUTH_FAILED;
68
69
70
return -EIO;
}
71
if (auth_state->challenge && *auth_state->challenge) {
72
int token_len = -EINVAL;
73
74
75
76
77
78
input_desc.cBuffers = 1;
input_desc.pBuffers = &in_token;
input_desc.ulVersion = SECBUFFER_VERSION;
in_token.BufferType = SECBUFFER_TOKEN;
79
in_token.pvBuffer = openconnect_base64_decode(&token_len,
80
auth_state->challenge);
81
if (!in_token.pvBuffer)
82
83
84
85
86
return token_len;
in_token.cbBuffer = token_len;
first = 0;
87
} else if (auth_state->state > AUTH_AVAILABLE) {
88
89
90
91
92
93
/* This indicates failure. We were trying, but got an empty
'Proxy-Authorization: Negotiate' header back from the server
implying that we should start again... */
goto fail_gssapi;
}
94
auth_state->state = AUTH_IN_PROGRESS;
95
96
97
98
99
100
101
102
103
output_desc.cBuffers = 1;
output_desc.pBuffers = &out_token;
output_desc.ulVersion = SECBUFFER_VERSION;
out_token.BufferType = SECBUFFER_TOKEN;
out_token.cbBuffer = 0;
out_token.pvBuffer = NULL;
104
105
106
status = InitializeSecurityContextW(&auth_state->sspi_cred,
first ? NULL : &auth_state->sspi_ctx,
auth_state->sspi_target_name,
107
108
109
ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_CONFIDENTIALITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONNECTION,
0, SECURITY_NETWORK_DREP,
first ? NULL : &input_desc,
110
0, &auth_state->sspi_ctx,
111
&output_desc, &ret_flags, NULL);
112
113
114
115
if (status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
vpn_progress(vpninfo, PRG_ERR,
_("InitializeSecurityContext() failed: %lx\n"), status);
fail_gssapi:
116
cleanup_gssapi_auth(vpninfo, auth_state);
117
auth_state->state = AUTH_FAILED;
118
119
120
121
/* -EAGAIN to first a reconnect if we had been trying. Else -EIO */
return first ? -EIO : -EAGAIN;
}
122
buf_append(hdrbuf, "%sAuthorization: Negotiate ", proxy ? "Proxy-" : "");
123
124
125
126
127
128
129
130
buf_append_base64(hdrbuf, out_token.pvBuffer, out_token.cbBuffer);
buf_append(hdrbuf, "\r\n");
FreeContextBuffer(out_token.pvBuffer);
return 0;
}
131
void cleanup_gssapi_auth(struct openconnect_info *vpninfo,
132
133
struct http_auth_state *auth_state)
134
{
135
if (auth_state->state >= AUTH_IN_PROGRESS) {
136
137
138
139
free(auth_state->sspi_target_name);
auth_state->sspi_target_name = NULL;
FreeCredentialsHandle(&auth_state->sspi_cred);
DeleteSecurityContext(&auth_state->sspi_ctx);
140
141
142
}
}
143
144
145
146
147
148
149
150
151
152
int socks_gssapi_auth(struct openconnect_info *vpninfo)
{
SECURITY_STATUS status;
SecBufferDesc input_desc, output_desc;
SecBuffer in_token, out_token;
ULONG ret_flags;
unsigned char *pktbuf;
int first = 1;
int i;
int ret = -EIO;
153
struct http_auth_state *auth_state = &vpninfo->proxy_auth[AUTH_TYPE_GSSAPI];
154
155
if (sspi_setup(vpninfo, auth_state, "rcmd", 1))
156
157
return -EIO;
158
vpninfo->proxy_auth[AUTH_TYPE_GSSAPI].state = AUTH_IN_PROGRESS;
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
pktbuf = malloc(65538);
if (!pktbuf)
return -ENOMEM;
input_desc.cBuffers = 1;
input_desc.pBuffers = &in_token;
input_desc.ulVersion = SECBUFFER_VERSION;
in_token.BufferType = SECBUFFER_TOKEN;
output_desc.cBuffers = 1;
output_desc.pBuffers = &out_token;
output_desc.ulVersion = SECBUFFER_VERSION;
out_token.BufferType = SECBUFFER_TOKEN;
out_token.cbBuffer = 0;
out_token.pvBuffer = NULL;
while (1) {
179
180
181
status = InitializeSecurityContextW(&auth_state->sspi_cred,
first ? NULL : &auth_state->sspi_ctx,
auth_state->sspi_target_name,
182
183
184
ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_CONFIDENTIALITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONNECTION,
0, SECURITY_NETWORK_DREP,
first ? NULL : &input_desc,
185
0, &auth_state->sspi_ctx,
186
&output_desc, &ret_flags, NULL);
187
if (status == SEC_E_OK) {
188
189
190
191
192
193
194
195
/* If we still have a token to send, send it. */
if (!out_token.cbBuffer) {
vpn_progress(vpninfo, PRG_DEBUG,
_("GSSAPI authentication completed\n"));
ret = 0;
break;
}
} else if (status != SEC_I_CONTINUE_NEEDED) {
196
197
198
199
200
201
202
203
204
205
206
207
208
209
vpn_progress(vpninfo, PRG_ERR,
_("InitializeSecurityContext() failed: %lx\n"), status);
break;
}
if (out_token.cbBuffer > 65535) {
vpn_progress(vpninfo, PRG_ERR,
_("SSPI token too large (%ld bytes)\n"),
out_token.cbBuffer);
break;
}
pktbuf[0] = 1; /* ver */
pktbuf[1] = 1; /* mtyp */
210
store_be16(pktbuf + 2, out_token.cbBuffer);
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
memcpy(pktbuf + 4, out_token.pvBuffer, out_token.cbBuffer);
FreeContextBuffer(out_token.pvBuffer);
vpn_progress(vpninfo, PRG_TRACE,
_("Sending SSPI token of %lu bytes\n"), out_token.cbBuffer + 4);
i = vpninfo->ssl_write(vpninfo, (void *)pktbuf, out_token.cbBuffer + 4);
if (i < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to send SSPI authentication token to proxy: %s\n"),
strerror(-i));
break;
}
i = vpninfo->ssl_read(vpninfo, (void *)pktbuf, 4);
if (i < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to receive SSPI authentication token from proxy: %s\n"),
strerror(-i));
break;
}
if (pktbuf[1] == 0xff) {
vpn_progress(vpninfo, PRG_ERR,
_("SOCKS server reported SSPI context failure\n"));
break;
} else if (pktbuf[1] != 1) {
vpn_progress(vpninfo, PRG_ERR,
_("Unknown SSPI status response (0x%02x) from SOCKS server\n"),
pktbuf[1]);
break;
}
243
in_token.cbBuffer = load_be16(pktbuf + 2);
244
245
246
in_token.pvBuffer = pktbuf;
first = 0;
247
248
249
250
251
252
253
if (!in_token.cbBuffer) {
vpn_progress(vpninfo, PRG_DEBUG,
_("GSSAPI authentication completed\n"));
ret = 0;
break;
}
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
i = vpninfo->ssl_read(vpninfo, (void *)pktbuf, in_token.cbBuffer);
if (i < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to receive SSPI authentication token from proxy: %s\n"),
strerror(-i));
break;
}
vpn_progress(vpninfo, PRG_TRACE, _("Got SSPI token of %lu bytes: %02x %02x %02x %02x\n"),
in_token.cbBuffer, pktbuf[0], pktbuf[1], pktbuf[2], pktbuf[3]);
}
if (!ret) {
SecPkgContext_Sizes sizes;
SecBufferDesc enc_desc;
SecBuffer enc_bufs[3];
int len;
ret = -EIO;
274
status = QueryContextAttributes(&auth_state->sspi_ctx, SECPKG_ATTR_SIZES, &sizes);
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
if (status != SEC_E_OK) {
vpn_progress(vpninfo, PRG_ERR,
_("QueryContextAttributes() failed: %lx\n"), status);
goto err;
}
enc_desc.cBuffers = 3;
enc_desc.pBuffers = enc_bufs;
enc_desc.ulVersion = SECBUFFER_VERSION;
enc_bufs[0].BufferType = SECBUFFER_TOKEN;
enc_bufs[0].cbBuffer = sizes.cbSecurityTrailer;
enc_bufs[0].pvBuffer = malloc(sizes.cbSecurityTrailer);
if (!enc_bufs[0].pvBuffer) {
ret = -ENOMEM;
goto err;
}
memset(enc_bufs[0].pvBuffer, 0, enc_bufs[0].cbBuffer);
enc_bufs[1].BufferType = SECBUFFER_DATA;
enc_bufs[1].pvBuffer = pktbuf;
enc_bufs[1].cbBuffer = 1;
/* All this just to sign this single byte... */
pktbuf[0] = 0;
enc_bufs[2].BufferType = SECBUFFER_PADDING;
enc_bufs[2].cbBuffer = sizes.cbBlockSize;
enc_bufs[2].pvBuffer = malloc(sizes.cbBlockSize);
if (!enc_bufs[2].pvBuffer) {
free(enc_bufs[0].pvBuffer);
ret = -ENOMEM;
goto err;
}
310
status = EncryptMessage(&auth_state->sspi_ctx, SECQOP_WRAP_NO_ENCRYPT, &enc_desc, 0);
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
if (status != SEC_E_OK) {
vpn_progress(vpninfo, PRG_ERR,
_("EncryptMessage() failed: %lx\n"), status);
free(enc_bufs[0].pvBuffer);
free(enc_bufs[2].pvBuffer);
goto err;
}
len = enc_bufs[0].cbBuffer + enc_bufs[1].cbBuffer + enc_bufs[2].cbBuffer;
/* Check each one to avoid the (utterly theoretical) overflow when calculated
into an 'int' type. */
if (enc_bufs[1].cbBuffer != 1 || enc_bufs[0].cbBuffer > 65535 ||
enc_bufs[2].cbBuffer > 65535 || len > 65535) {
vpn_progress(vpninfo, PRG_ERR,
_("EncryptMessage() result too large (%lu + %lu + %lu)\n"),
enc_bufs[0].cbBuffer, enc_bufs[1].cbBuffer, enc_bufs[2].cbBuffer);
free(enc_bufs[0].pvBuffer);
free(enc_bufs[2].pvBuffer);
goto err;
}
332
333
334
335
/* Our single byte of payload was *supposed* to be unencrypted but
Windows doesn't always manage to do as it's told... */
pktbuf[4 + enc_bufs[0].cbBuffer] = pktbuf[0];
336
337
pktbuf[0] = 1;
pktbuf[1] = 2;
338
store_be16(pktbuf + 2, len);
339
340
341
if (enc_bufs[0].cbBuffer)
memcpy(pktbuf + 4, enc_bufs[0].pvBuffer, enc_bufs[0].cbBuffer);
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
if (enc_bufs[2].cbBuffer)
memcpy(pktbuf + 5 + enc_bufs[0].cbBuffer, enc_bufs[2].pvBuffer, enc_bufs[2].cbBuffer);
free(enc_bufs[0].pvBuffer);
free(enc_bufs[2].pvBuffer);
vpn_progress(vpninfo, PRG_TRACE,
_("Sending SSPI protection negotiation of %u bytes\n"), len + 4);
i = vpninfo->ssl_write(vpninfo, (void *)pktbuf, len + 4);
if (i < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to send SSPI protection response to proxy: %s\n"),
strerror(-i));
goto err;
}
i = vpninfo->ssl_read(vpninfo, (void *)pktbuf, 4);
if (i < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to receive SSPI protection response from proxy: %s\n"),
strerror(-i));
goto err;
}
368
len = load_be16(pktbuf + 2);
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
i = vpninfo->ssl_read(vpninfo, (void *)pktbuf, len);
if (i < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to receive SSPI protection response from proxy: %s\n"),
strerror(-i));
goto err;
}
vpn_progress(vpninfo, PRG_TRACE,
_("Got SSPI protection response of %d bytes: %02x %02x %02x %02x\n"),
len, pktbuf[0], pktbuf[1], pktbuf[2], pktbuf[3]);
enc_desc.cBuffers = 2;
enc_bufs[0].BufferType = SECBUFFER_STREAM;
enc_bufs[0].cbBuffer = len;
enc_bufs[0].pvBuffer = pktbuf;
enc_bufs[1].BufferType = SECBUFFER_DATA;
enc_bufs[1].cbBuffer = 0;
enc_bufs[1].pvBuffer = NULL;
391
status = DecryptMessage(&auth_state->sspi_ctx, &enc_desc, 0, NULL);
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
if (status != SEC_E_OK) {
vpn_progress(vpninfo, PRG_ERR,
_("DecryptMessage failed: %lx\n"), status);
goto err;
}
if (enc_bufs[1].cbBuffer != 1) {
vpn_progress(vpninfo, PRG_ERR,
_("Invalid SSPI protection response from proxy (%lu bytes)\n"),
enc_bufs[1].cbBuffer);
FreeContextBuffer(enc_bufs[1].pvBuffer);
goto err;
}
i = *(char *)enc_bufs[1].pvBuffer;
if (i == 1) {
vpn_progress(vpninfo, PRG_ERR,
_("SOCKS proxy demands message integrity, which is not supported\n"));
goto err;
} else if (i == 2) {
vpn_progress(vpninfo, PRG_ERR,
_("SOCKS proxy demands message confidentiality, which is not supported\n"));
goto err;
} else if (i) {
vpn_progress(vpninfo, PRG_ERR,
_("SOCKS proxy demands protection unknown type 0x%02x\n"),
(unsigned char)i);
goto err;
}
ret = 0;
}
err:
425
cleanup_gssapi_auth(vpninfo, &vpninfo->proxy_auth[AUTH_TYPE_GSSAPI]);
426
vpninfo->proxy_auth[AUTH_TYPE_GSSAPI].state = AUTH_UNSEEN;
427
428
429
430
free(pktbuf);
return ret;
}