Skip to content

Latest commit

 

History

History
1340 lines (1175 loc) · 38.3 KB

oncp.c

File metadata and controls

1340 lines (1175 loc) · 38.3 KB
 
Jan 26, 2015
Jan 26, 2015
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2008-2015 Intel Corporation.
*
* 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.
*/
Jan 26, 2015
Jan 26, 2015
18
19
20
21
/*
* Grateful thanks to Tiebing Zhang, who did much of the hard work
* of analysing and decoding the protocol.
*/
Feb 6, 2015
Feb 6, 2015
22
Jan 26, 2015
Jan 26, 2015
23
24
25
26
27
28
29
30
31
32
33
34
#include <config.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdarg.h>
Jan 26, 2015
Jan 26, 2015
35
#include <sys/types.h>
Jan 26, 2015
Jan 26, 2015
36
Jan 26, 2015
Jan 26, 2015
37
38
#include "openconnect-internal.h"
Jan 26, 2015
Jan 26, 2015
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
static int parse_cookie(struct openconnect_info *vpninfo)
{
char *p = vpninfo->cookie;
/* We currenly expect the "cookie" to be contain multiple cookies:
* DSSignInUrl=/; DSID=xxx; DSFirstAccess=xxx; DSLastAccess=xxx
* Process those into vpninfo->cookies unless we already had them
* (in which case they'll may be newer. */
while (p && *p) {
char *semicolon = strchr(p, ';');
char *equals;
if (semicolon)
*semicolon = 0;
equals = strchr(p, '=');
if (!equals) {
vpn_progress(vpninfo, PRG_ERR, _("Invalid cookie '%s'\n"), p);
return -EINVAL;
}
*equals = 0;
http_add_cookie(vpninfo, p, equals+1, 0);
*equals = '=';
p = semicolon;
if (p) {
*p = ';';
p++;
Jan 26, 2015
Jan 26, 2015
67
while (*p && isspace((int)(unsigned char)*p))
Jan 26, 2015
Jan 26, 2015
68
69
70
71
72
73
p++;
}
}
return 0;
}
Jan 26, 2015
Jan 26, 2015
74
Jan 26, 2015
Jan 26, 2015
75
76
77
78
static void buf_append_be16(struct oc_text_buf *buf, uint16_t val)
{
unsigned char b[2];
Jan 26, 2015
Jan 26, 2015
79
store_be16(b, val);
Jan 26, 2015
Jan 26, 2015
80
81
82
83
buf_append_bytes(buf, b, 2);
}
Jan 26, 2015
Jan 26, 2015
84
85
86
87
static void buf_append_le16(struct oc_text_buf *buf, uint16_t val)
{
unsigned char b[2];
Jan 26, 2015
Jan 26, 2015
88
store_le16(b, val);
Jan 26, 2015
Jan 26, 2015
89
90
91
92
buf_append_bytes(buf, b, 2);
}
Jan 26, 2015
Jan 26, 2015
93
94
95
96
static void buf_append_tlv(struct oc_text_buf *buf, uint16_t val, uint32_t len, void *data)
{
unsigned char b[6];
Jan 26, 2015
Jan 26, 2015
97
98
store_be16(b, val);
store_be32(b + 2, len);
Jan 26, 2015
Jan 26, 2015
99
100
101
102
103
104
105
106
107
buf_append_bytes(buf, b, 6);
if (len)
buf_append_bytes(buf, data, len);
}
static void buf_append_tlv_be32(struct oc_text_buf *buf, uint16_t val, uint32_t data)
{
unsigned char d[4];
Jan 26, 2015
Jan 26, 2015
108
store_be32(d, data);
Jan 26, 2015
Jan 26, 2015
109
110
111
112
buf_append_tlv(buf, val, 4, d);
}
Jan 26, 2015
Jan 26, 2015
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
static const char authpkt_head[] = { 0x00, 0x04, 0x00, 0x00, 0x00 };
static const char authpkt_tail[] = { 0xbb, 0x01, 0x00, 0x00, 0x00, 0x00 };
#define GRP_ATTR(g, a) (((g) << 16) | (a))
/* We behave like CSTP — create a linked list in vpninfo->cstp_options
* with the strings containing the information we got from the server,
* and oc_ip_info contains const copies of those pointers. */
static const char *add_option(struct openconnect_info *vpninfo, const char *opt,
const char *val, int val_len)
{
struct oc_vpn_option *new = malloc(sizeof(*new));
if (!new)
return NULL;
new->option = strdup(opt);
if (!new->option) {
free(new);
return NULL;
}
if (val_len >= 0)
new->value = strndup(val, val_len);
else
new->value = strdup(val);
if (!new->value) {
free(new->option);
free(new);
return NULL;
}
new->next = vpninfo->cstp_options;
vpninfo->cstp_options = new;
return new->value;
}
static int process_attr(struct openconnect_info *vpninfo, int group, int attr,
unsigned char *data, int attrlen)
{
char buf[80];
int i;
switch(GRP_ATTR(group, attr)) {
case GRP_ATTR(6, 2):
if (attrlen != 4) {
badlen:
vpn_progress(vpninfo, PRG_ERR,
_("Unexpected length %d for TLV %d/%d\n"),
attrlen, group, attr);
return -EINVAL;
}
Jan 26, 2015
Jan 26, 2015
164
vpninfo->ip_info.mtu = load_be32(data);
Jan 26, 2015
Jan 26, 2015
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
vpn_progress(vpninfo, PRG_DEBUG,
_("Received MTU %d from server\n"),
vpninfo->ip_info.mtu);
break;
case GRP_ATTR(2, 1):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS server %s\n"), buf);
for (i = 0; i < 3; i++) {
if (!vpninfo->ip_info.dns[i]) {
vpninfo->ip_info.dns[i] = add_option(vpninfo, "DNS", buf, -1);
break;
}
}
break;
case GRP_ATTR(2, 2):
Jan 26, 2015
Jan 26, 2015
186
vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS search domain %.*s\n"),
Jan 26, 2015
Jan 26, 2015
187
188
attrlen, (char *)data);
vpninfo->ip_info.domain = add_option(vpninfo, "search", (char *)data, attrlen);
Aug 6, 2015
Aug 6, 2015
189
190
191
192
193
if (vpninfo->ip_info.domain) {
char *p = (char *)vpninfo->ip_info.domain;
while ((p = strchr(p, ',')))
*p = ' ';
}
Jan 26, 2015
Jan 26, 2015
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
break;
case GRP_ATTR(1, 1):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received internal IP address %s\n"), buf);
vpninfo->ip_info.addr = add_option(vpninfo, "ipaddr", buf, -1);
break;
case GRP_ATTR(1, 2):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received netmask %s\n"), buf);
Jan 26, 2015
Jan 26, 2015
211
vpninfo->ip_info.netmask = add_option(vpninfo, "netmask", buf, -1);
Jan 26, 2015
Jan 26, 2015
212
213
214
215
216
217
218
219
220
221
222
223
224
break;
case GRP_ATTR(1, 3):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received internal gateway address %s\n"), buf);
/* Hm, what are we supposed to do with this? It's a tunnel;
having a gateway is meaningless. */
add_option(vpninfo, "ipaddr", buf, -1);
break;
Jan 26, 2015
Jan 26, 2015
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
case GRP_ATTR(3, 3): {
struct oc_split_include *inc;
if (attrlen != 8)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d/%d.%d.%d.%d",
data[0], data[1], data[2], data[3],
data[4], data[5], data[6], data[7]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received split include route %s\n"), buf);
if (!data[4] && !data[5] && !data[6] && !data[7])
break;
inc = malloc(sizeof(*inc));
if (inc) {
inc->route = add_option(vpninfo, "split-include", buf, -1);
if (inc->route) {
inc->next = vpninfo->ip_info.split_includes;
vpninfo->ip_info.split_includes = inc;
} else
free(inc);
}
break;
}
Feb 13, 2015
Feb 13, 2015
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
case GRP_ATTR(3, 4): {
struct oc_split_include *exc;
if (attrlen != 8)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d/%d.%d.%d.%d",
data[0], data[1], data[2], data[3],
data[4], data[5], data[6], data[7]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received split exclude route %s\n"), buf);
if (!data[4] && !data[5] && !data[6] && !data[7])
break;
exc = malloc(sizeof(*exc));
if (exc) {
exc->route = add_option(vpninfo, "split-exclude", buf, -1);
if (exc->route) {
exc->next = vpninfo->ip_info.split_excludes;
vpninfo->ip_info.split_excludes = exc;
} else
free(exc);
}
break;
}
Jan 26, 2015
Jan 26, 2015
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
case GRP_ATTR(4, 1):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received WINS server %s\n"), buf);
for (i = 0; i < 3; i++) {
if (!vpninfo->ip_info.nbns[i]) {
vpninfo->ip_info.nbns[i] = add_option(vpninfo, "WINS", buf, -1);
break;
}
}
break;
case GRP_ATTR(8, 1): {
const char *enctype;
if (attrlen != 1)
goto badlen;
May 15, 2017
May 15, 2017
289
if (data[0] == ENC_AES_128_CBC) {
Jan 26, 2015
Jan 26, 2015
290
enctype = "AES-128";
May 15, 2017
May 15, 2017
291
292
vpninfo->enc_key_len = 16;
} else if (data[0] == ENC_AES_256_CBC) {
Jan 26, 2015
Jan 26, 2015
293
enctype = "AES-256";
May 15, 2017
May 15, 2017
294
295
vpninfo->enc_key_len = 32;
} else
Jan 26, 2015
Jan 26, 2015
296
297
298
enctype = "unknown";
vpn_progress(vpninfo, PRG_DEBUG, _("ESP encryption: 0x%02x (%s)\n"),
data[0], enctype);
Jan 26, 2015
Jan 26, 2015
299
vpninfo->esp_enc = data[0];
Jan 26, 2015
Jan 26, 2015
300
301
302
break;
}
Jan 26, 2015
Jan 26, 2015
303
304
305
306
307
case GRP_ATTR(8, 2): {
const char *mactype;
if (attrlen != 1)
goto badlen;
May 15, 2017
May 15, 2017
308
if (data[0] == HMAC_MD5) {
Jan 26, 2015
Jan 26, 2015
309
mactype = "MD5";
May 15, 2017
May 15, 2017
310
311
vpninfo->hmac_key_len = 16;
} else if (data[0] == HMAC_SHA1) {
Jan 26, 2015
Jan 26, 2015
312
mactype = "SHA1";
May 15, 2017
May 15, 2017
313
314
vpninfo->hmac_key_len = 20;
} else
Jan 26, 2015
Jan 26, 2015
315
316
317
318
319
320
321
mactype = "unknown";
vpn_progress(vpninfo, PRG_DEBUG, _("ESP HMAC: 0x%02x (%s)\n"),
data[0], mactype);
vpninfo->esp_hmac = data[0];
break;
}
Jan 26, 2015
Jan 26, 2015
322
323
324
case GRP_ATTR(8, 3):
if (attrlen != 1)
goto badlen;
Jan 26, 2015
Jan 26, 2015
325
vpninfo->esp_compr = data[0];
Aug 2, 2018
Aug 2, 2018
326
vpninfo->dtls_compr = data[0] ? COMPR_LZO : 0;
Jan 26, 2015
Jan 26, 2015
327
328
329
vpn_progress(vpninfo, PRG_DEBUG, _("ESP compression: %d\n"), data[0]);
break;
Jan 26, 2015
Jan 26, 2015
330
331
332
case GRP_ATTR(8, 4):
if (attrlen != 2)
goto badlen;
Jan 26, 2015
Jan 26, 2015
333
i = load_be16(data);
Jan 26, 2015
Jan 26, 2015
334
335
udp_sockaddr(vpninfo, i);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP port: %d\n"), i);
Jan 26, 2015
Jan 26, 2015
336
337
338
339
340
break;
case GRP_ATTR(8, 5):
if (attrlen != 4)
goto badlen;
Jan 26, 2015
Jan 26, 2015
341
vpninfo->esp_lifetime_bytes = load_be32(data);
Jan 26, 2015
Jan 26, 2015
342
343
vpn_progress(vpninfo, PRG_DEBUG, _("ESP key lifetime: %u bytes\n"),
vpninfo->esp_lifetime_bytes);
Jan 26, 2015
Jan 26, 2015
344
345
346
347
348
break;
case GRP_ATTR(8, 6):
if (attrlen != 4)
goto badlen;
Jan 26, 2015
Jan 26, 2015
349
vpninfo->esp_lifetime_seconds = load_be32(data);
Jan 26, 2015
Jan 26, 2015
350
351
vpn_progress(vpninfo, PRG_DEBUG, _("ESP key lifetime: %u seconds\n"),
vpninfo->esp_lifetime_seconds);
Jan 26, 2015
Jan 26, 2015
352
353
break;
Jan 26, 2015
Jan 26, 2015
354
case GRP_ATTR(8, 9):
Jan 26, 2015
Jan 26, 2015
355
356
if (attrlen != 4)
goto badlen;
Jan 26, 2015
Jan 26, 2015
357
vpninfo->esp_ssl_fallback = load_be32(data);
Jan 26, 2015
Jan 26, 2015
358
359
vpn_progress(vpninfo, PRG_DEBUG, _("ESP to SSL fallback: %u seconds\n"),
vpninfo->esp_ssl_fallback);
Jan 26, 2015
Jan 26, 2015
360
361
break;
Jan 26, 2015
Jan 26, 2015
362
363
364
case GRP_ATTR(8, 10):
if (attrlen != 4)
goto badlen;
Jan 26, 2015
Jan 26, 2015
365
vpninfo->esp_replay_protect = load_be32(data);
Jan 26, 2015
Jan 26, 2015
366
vpn_progress(vpninfo, PRG_DEBUG, _("ESP replay protection: %d\n"),
Jan 26, 2015
Jan 26, 2015
367
load_be32(data));
Jan 26, 2015
Jan 26, 2015
368
369
break;
Jan 26, 2015
Jan 26, 2015
370
371
372
case GRP_ATTR(7, 1):
if (attrlen != 4)
goto badlen;
Jan 26, 2015
Jan 26, 2015
373
memcpy(&vpninfo->esp_out.spi, data, 4);
Jan 26, 2015
Jan 26, 2015
374
vpn_progress(vpninfo, PRG_DEBUG, _("ESP SPI (outbound): %x\n"),
Jan 26, 2015
Jan 26, 2015
375
load_be32(data));
Jan 26, 2015
Jan 26, 2015
376
377
378
break;
case GRP_ATTR(7, 2):
Jan 26, 2015
Jan 26, 2015
379
380
if (attrlen != 0x40)
goto badlen;
May 15, 2017
May 15, 2017
381
382
/* data contains enc_key and hmac_key concatenated */
memcpy(vpninfo->esp_out.enc_key, data, 0x40);
Jan 26, 2015
Jan 26, 2015
383
384
385
386
vpn_progress(vpninfo, PRG_DEBUG, _("%d bytes of ESP secrets\n"),
attrlen);
break;
Jan 26, 2015
Jan 26, 2015
387
388
389
390
391
392
393
394
395
396
397
398
399
400
default:
buf[0] = 0;
for (i=0; i < 16 && i < attrlen; i++)
sprintf(buf + strlen(buf), " %02x", data[i]);
if (attrlen > 16)
sprintf(buf + strlen(buf), "...");
vpn_progress(vpninfo, PRG_DEBUG,
_("Unknown TLV group %d attr %d len %d:%s\n"),
group, attr, attrlen, buf);
}
return 0;
}
Jan 26, 2015
Jan 26, 2015
401
402
403
404
static void put_len16(struct oc_text_buf *buf, int where)
{
int len = buf->pos - where;
Jan 26, 2015
Jan 26, 2015
405
store_be16(buf->data + where - 2, len);
Jan 26, 2015
Jan 26, 2015
406
407
408
409
410
411
}
static void put_len32(struct oc_text_buf *buf, int where)
{
int len = buf->pos - where;
Jan 26, 2015
Jan 26, 2015
412
store_be32(buf->data + where - 4, len);
Jan 26, 2015
Jan 26, 2015
413
414
415
416
417
418
419
420
421
}
/* We don't know what these are so just hope they never change */
static const unsigned char kmp_head[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char kmp_tail[] = { 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char kmp_tail_out[] = { 0x01, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00 };
Jan 26, 2015
Jan 26, 2015
422
423
424
static const unsigned char data_hdr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x2c, 0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
Jan 26, 2015
Jan 26, 2015
425
Sep 10, 2016
Sep 10, 2016
426
#ifdef HAVE_ESP
Jan 26, 2015
Jan 26, 2015
427
428
429
430
431
432
433
434
435
436
437
438
static const unsigned char esp_kmp_hdr[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2e,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, /* KMP header */
0x00, 0x56, /* KMP length */
0x00, 0x07, 0x00, 0x00, 0x00, 0x50, /* TLV group 7 */
0x00, 0x01, 0x00, 0x00, 0x00, 0x04, /* Attr 1 (SPI) */
};
/* Followed by 4 bytes of SPI */
static const unsigned char esp_kmp_part2[] = {
0x00, 0x02, 0x00, 0x00, 0x00, 0x40, /* Attr 2 (secrets) */
};
/* And now 0x40 bytes of random secret for encryption and HMAC key */
Jul 26, 2016
Jul 26, 2016
439
#endif
Jan 26, 2015
Jan 26, 2015
440
Jan 26, 2015
Jan 26, 2015
441
static const struct pkt esp_enable_pkt = {
Jan 26, 2015
Jan 26, 2015
442
.next = NULL,
Aug 30, 2016
Aug 30, 2016
443
444
445
446
{ .oncp = { .rec = { 0x21, 0x00 },
.kmp = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2f,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0d } }
Mar 14, 2015
Mar 14, 2015
447
},
Jan 26, 2015
Jan 26, 2015
448
449
450
451
452
453
454
455
.data = {
0x00, 0x06, 0x00, 0x00, 0x00, 0x07, /* Group 6, len 7 */
0x00, 0x01, 0x00, 0x00, 0x00, 0x01, /* Attr 1, len 1 */
0x01
},
.len = 13
};
May 30, 2018
May 30, 2018
456
static int queue_esp_control(struct openconnect_info *vpninfo, int enable)
Feb 2, 2015
Feb 2, 2015
457
458
459
460
461
462
463
464
465
466
467
{
struct pkt *new = malloc(sizeof(*new) + 13);
if (!new)
return -ENOMEM;
memcpy(new, &esp_enable_pkt, sizeof(*new) + 13);
new->data[12] = enable;
queue_packet(&vpninfo->oncp_control_queue, new);
return 0;
}
Jan 26, 2015
Jan 26, 2015
468
469
470
471
472
473
474
475
static int check_kmp_header(struct openconnect_info *vpninfo, unsigned char *bytes, int pktlen)
{
if (pktlen < 20 || memcmp(bytes, kmp_head, sizeof(kmp_head)) ||
memcmp(bytes + 8, kmp_tail, sizeof(kmp_tail))) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to parse KMP header\n"));
return -EINVAL;
}
Jan 26, 2015
Jan 26, 2015
476
return load_be16(bytes + 6);
Jan 26, 2015
Jan 26, 2015
477
478
479
480
481
482
}
static int parse_conf_pkt(struct openconnect_info *vpninfo, unsigned char *bytes, int pktlen, int kmp)
{
int kmplen, kmpend, grouplen, groupend, group, attr, attrlen;
int ofs = 0;
May 15, 2017
May 15, 2017
483
int split_enc_hmac_keys = 0;
Jan 26, 2015
Jan 26, 2015
484
Jan 26, 2015
Jan 26, 2015
485
kmplen = load_be16(bytes + ofs + 18);
Jan 26, 2015
Jan 26, 2015
486
487
488
489
490
kmpend = ofs + kmplen;
if (kmpend > pktlen) {
eparse:
vpn_progress(vpninfo, PRG_ERR,
_("Failed to parse KMP message\n"));
Aug 14, 2017
Aug 14, 2017
491
dump_buf_hex(vpninfo, PRG_ERR, '<', bytes, pktlen);
Jan 26, 2015
Jan 26, 2015
492
493
494
495
496
497
498
499
500
501
502
return -EINVAL;
}
vpn_progress(vpninfo, PRG_DEBUG,
_("Got KMP message %d of size %d\n"),
kmp, kmplen);
ofs += 0x14;
while (ofs < kmpend) {
if (ofs + 6 > kmpend)
goto eparse;
Jan 26, 2015
Jan 26, 2015
503
504
group = load_be16(bytes + ofs);
grouplen = load_be32(bytes + ofs + 2);
Jan 26, 2015
Jan 26, 2015
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
ofs += 6;
groupend = ofs + grouplen;
if (groupend > pktlen)
goto eparse;
if (kmp == 302 && group != 7 && group != 8) {
vpn_progress(vpninfo, PRG_ERR,
_("Received non-ESP TLVs (group %d) in ESP negotiation KMP\n"),
group);
return -EINVAL;
}
while (ofs < groupend) {
if (ofs + 6 > groupend)
goto eparse;
Jan 26, 2015
Jan 26, 2015
520
521
attr = load_be16(bytes + ofs);
attrlen = load_be32(bytes + ofs + 2);
Jan 26, 2015
Jan 26, 2015
522
523
524
525
526
ofs += 6;
if (attrlen + ofs > groupend)
goto eparse;
if (process_attr(vpninfo, group, attr, bytes + ofs, attrlen))
goto eparse;
May 15, 2017
May 15, 2017
527
528
if (GRP_ATTR(group, attr)==GRP_ATTR(7, 2))
split_enc_hmac_keys = 1;
Jan 26, 2015
Jan 26, 2015
529
530
531
ofs += attrlen;
}
}
May 15, 2017
May 15, 2017
532
533
534
535
536
537
/* The encryption and HMAC keys are sent concatenated together in a block of 0x40 bytes;
we can't split them apart until we know how long the encryption key is. */
if (split_enc_hmac_keys)
memcpy(vpninfo->esp_out.hmac_key, vpninfo->esp_out.enc_key + vpninfo->enc_key_len, vpninfo->hmac_key_len);
Jan 26, 2015
Jan 26, 2015
538
539
540
return 0;
}
May 15, 2017
May 15, 2017
541
Jan 26, 2015
Jan 26, 2015
542
543
int oncp_connect(struct openconnect_info *vpninfo)
{
Aug 14, 2017
Aug 14, 2017
544
int ret, len, kmp, kmplen, group, check_len;
Jan 26, 2015
Jan 26, 2015
545
struct oc_text_buf *reqbuf;
Jul 24, 2019
Jul 24, 2019
546
unsigned char bytes[65536];
Apr 24, 2015
Apr 24, 2015
547
Jan 26, 2015
Jan 26, 2015
548
549
/* XXX: We should do what cstp_connect() does to check that configuration
hasn't changed on a reconnect. */
Jan 26, 2015
Jan 26, 2015
550
551
552
553
554
555
556
557
558
559
if (!vpninfo->cookies) {
ret = parse_cookie(vpninfo);
if (ret)
return ret;
}
ret = openconnect_open_https(vpninfo);
if (ret)
return ret;
Jan 26, 2015
Jan 26, 2015
560
Jan 26, 2015
Jan 26, 2015
561
reqbuf = buf_alloc();
Jan 26, 2015
Jan 26, 2015
562
Jan 26, 2015
Jan 26, 2015
563
buf_append(reqbuf, "POST /dana/js?prot=1&svc=4 HTTP/1.1\r\n");
Aug 2, 2018
Aug 2, 2018
564
565
566
567
568
569
570
/* The TLS socket actually remains open for use by the oNCP
tunnel, but the "Connection: close" header is nevertheless
required here. It appears to signal to the server to stop
treating this as an HTTP connection and to start treating
it as an oNCP connection.
*/
buf_append(reqbuf, "Connection: close\r\n");
Jan 26, 2015
Jan 26, 2015
571
oncp_common_headers(vpninfo, reqbuf);
Sep 22, 2016
Sep 22, 2016
572
buf_append(reqbuf, "Content-Length: 256\r\n");
Jan 26, 2015
Jan 26, 2015
573
574
575
576
577
buf_append(reqbuf, "\r\n");
if (buf_error(reqbuf)) {
vpn_progress(vpninfo, PRG_ERR,
_("Error creating oNCP negotiation request\n"));
Jan 26, 2015
Jan 26, 2015
578
579
ret = buf_error(reqbuf);
goto out;
Jan 26, 2015
Jan 26, 2015
580
581
}
ret = vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos);
Jan 26, 2015
Jan 26, 2015
582
583
584
if (ret < 0)
goto out;
Jan 26, 2015
Jan 26, 2015
585
ret = process_http_response(vpninfo, 1, NULL, reqbuf);
Jan 26, 2015
Jan 26, 2015
586
587
588
if (ret < 0)
goto out;
Jan 26, 2015
Jan 26, 2015
589
590
591
592
if (ret != 200) {
vpn_progress(vpninfo, PRG_ERR,
_("Unexpected %d result from server\n"),
ret);
Jan 26, 2015
Jan 26, 2015
593
594
ret = -EINVAL;
goto out;
Jan 26, 2015
Jan 26, 2015
595
596
}
Jan 26, 2015
Jan 26, 2015
597
598
599
/* This is probably some kind of vestigial authentication packet, although
* it's mostly obsolete now that the authentication is really done over
* HTTP. We only send the hostname. */
Jan 26, 2015
Jan 26, 2015
600
buf_truncate(reqbuf);
Jan 26, 2015
Jan 26, 2015
601
602
603
604
605
606
607
608
609
buf_append_le16(reqbuf, sizeof(authpkt_head) + 2 +
strlen(vpninfo->localname) + sizeof(authpkt_tail));
buf_append_bytes(reqbuf, authpkt_head, sizeof(authpkt_head));
buf_append_le16(reqbuf, strlen(vpninfo->localname));
buf_append(reqbuf, "%s", vpninfo->localname);
buf_append_bytes(reqbuf, authpkt_tail, sizeof(authpkt_tail));
if (buf_error(reqbuf)) {
vpn_progress(vpninfo, PRG_ERR,
_("Error creating oNCP negotiation request\n"));
Jan 26, 2015
Jan 26, 2015
610
611
ret = buf_error(reqbuf);
goto out;
Jan 26, 2015
Jan 26, 2015
612
}
Aug 14, 2017
Aug 14, 2017
613
dump_buf_hex(vpninfo, PRG_DEBUG, '>', (void *)reqbuf->data, reqbuf->pos);
Jan 26, 2015
Jan 26, 2015
614
ret = vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos);
Jan 26, 2015
Jan 26, 2015
615
616
617
618
619
620
if (ret != reqbuf->pos) {
if (ret >= 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Short write in oNCP negotiation\n"));
ret = -EIO;
}
Jan 26, 2015
Jan 26, 2015
621
goto out;
Jan 26, 2015
Jan 26, 2015
622
}
Jan 26, 2015
Jan 26, 2015
623
624
625
626
/* Now we expect a three-byte response with what's presumably an
error code */
ret = vpninfo->ssl_read(vpninfo, (void *)bytes, 3);
Aug 14, 2017
Aug 14, 2017
627
check_len = load_le16(bytes);
Jan 26, 2015
Jan 26, 2015
628
629
if (ret < 0)
goto out;
Jan 26, 2015
Jan 26, 2015
630
631
vpn_progress(vpninfo, PRG_TRACE,
_("Read %d bytes of SSL record\n"), ret);
Aug 14, 2017
Aug 14, 2017
632
dump_buf_hex(vpninfo, PRG_TRACE, '<', (void *)bytes, ret);
Aug 14, 2017
Aug 14, 2017
633
Aug 14, 2017
Aug 14, 2017
634
if (ret != 3 || check_len < 1) {
Jan 26, 2015
Jan 26, 2015
635
636
637
vpn_progress(vpninfo, PRG_ERR,
_("Unexpected response of size %d after hostname packet\n"),
ret);
Jan 26, 2015
Jan 26, 2015
638
639
ret = -EINVAL;
goto out;
Jan 26, 2015
Jan 26, 2015
640
641
642
643
644
}
if (bytes[2]) {
vpn_progress(vpninfo, PRG_ERR,
_("Server response to hostname packet is error 0x%02x\n"),
bytes[2]);
Mar 30, 2020
Mar 30, 2020
645
646
647
648
649
650
if (bytes[2] == 0x08)
vpn_progress(vpninfo, PRG_ERR,
_("This seems to indicate that the server has disabled support for\n"
"Juniper's older oNCP protocol, and only allows connections using\n"
"the newer Junos Pulse protocol. This version of OpenConnect has\n"
"EXPERIMENTAL support for Pulse using --prot=pulse\n"));
Jan 26, 2015
Jan 26, 2015
651
652
ret = -EINVAL;
goto out;
Jan 26, 2015
Jan 26, 2015
653
654
}
Aug 14, 2017
Aug 14, 2017
655
656
657
658
659
660
661
662
663
664
665
666
/* And then a KMP message 301 with the IP configuration.
* Sometimes this arrives as a separate SSL record (with its own
* 2-byte length prefix), and sometimes concatenated with the
* previous 3-byte response).
*/
if (check_len == 1) {
len = vpninfo->ssl_read(vpninfo, (void *)bytes, sizeof(bytes));
check_len = load_le16(bytes);
} else {
len = vpninfo->ssl_read(vpninfo, (void *)(bytes+2), sizeof(bytes)-2) + 2;
check_len--;
}
Jan 26, 2015
Jan 26, 2015
667
668
if (len < 0) {
ret = len;
Jan 26, 2015
Jan 26, 2015
669
goto out;
Jan 26, 2015
Jan 26, 2015
670
}
Jan 26, 2015
Jan 26, 2015
671
vpn_progress(vpninfo, PRG_TRACE,
Jan 26, 2015
Jan 26, 2015
672
_("Read %d bytes of SSL record\n"), len);
Jan 26, 2015
Jan 26, 2015
673
Aug 14, 2017
Aug 14, 2017
674
if (len < 0x16 || check_len + 2 != len) {
Jan 26, 2015
Jan 26, 2015
675
676
vpn_progress(vpninfo, PRG_ERR,
_("Invalid packet waiting for KMP 301\n"));
Aug 14, 2017
Aug 14, 2017
677
dump_buf_hex(vpninfo, PRG_ERR, '<', bytes, len);
Jan 26, 2015
Jan 26, 2015
678
679
ret = -EINVAL;
goto out;
Jan 26, 2015
Jan 26, 2015
680
681
}
Jan 26, 2015
Jan 26, 2015
682
683
684
ret = check_kmp_header(vpninfo, bytes + 2, len);
if (ret < 0)
goto out;
Jan 26, 2015
Jan 26, 2015
685
Jan 26, 2015
Jan 26, 2015
686
687
688
689
690
691
692
/* We expect KMP message 301 here */
if (ret != 301) {
vpn_progress(vpninfo, PRG_ERR,
_("Expected KMP message 301 from server but got %d\n"),
ret);
ret = -EINVAL;
goto out;
Jan 26, 2015
Jan 26, 2015
693
}
Jan 26, 2015
Jan 26, 2015
694
Apr 24, 2015
Apr 24, 2015
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
kmplen = load_be16(bytes + 20);
if (kmplen + 2 >= sizeof(bytes)) {
vpn_progress(vpninfo, PRG_ERR,
_("KMP message 301 from server too large (%d bytes)\n"),
kmplen);
ret = -EINVAL;
goto out;
}
vpn_progress(vpninfo, PRG_TRACE,
_("Got KMP message 301 of length %d\n"), kmplen);
while (kmplen + 22 > len) {
char l[2];
int thislen;
if (vpninfo->ssl_read(vpninfo, (void *)l, 2) != 2) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read continuation record length\n"));
ret = -EINVAL;
goto out;
}
if (load_le16(l) + len > kmplen + 22) {
vpn_progress(vpninfo, PRG_ERR,
_("Record of additional %d bytes too large; would make %d\n"),
load_le16(l), len + load_le16(l));
ret = -EINVAL;
goto out;
}
thislen = vpninfo->ssl_read(vpninfo, (void *)(bytes + len), load_le16(l));
if (thislen != load_le16(l)) {
vpn_progress(vpninfo, PRG_ERR,
Jun 8, 2015
Jun 8, 2015
726
_("Failed to read continuation record of length %d\n"),
Apr 24, 2015
Apr 24, 2015
727
728
729
730
731
732
733
734
735
736
737
load_le16(l));
ret = -EINVAL;
goto out;
}
vpn_progress(vpninfo, PRG_TRACE,
_("Read additional %d bytes of KMP 301 message\n"),
thislen);
len += thislen;
}
ret = parse_conf_pkt(vpninfo, bytes + 2, len - 2, ret);
Jan 26, 2015
Jan 26, 2015
738
739
740
if (ret)
goto out;
Jan 26, 2015
Jan 26, 2015
741
742
743
buf_truncate(reqbuf);
buf_append_le16(reqbuf, 0); /* Length. We'll fix it later. */
buf_append_bytes(reqbuf, kmp_head, sizeof(kmp_head));
Jan 26, 2015
Jan 26, 2015
744
buf_append_be16(reqbuf, 303); /* KMP message 303 */
Jan 26, 2015
Jan 26, 2015
745
buf_append_bytes(reqbuf, kmp_tail_out, sizeof(kmp_tail_out));
Jan 26, 2015
Jan 26, 2015
746
buf_append_be16(reqbuf, 0); /* KMP message length */
Jan 26, 2015
Jan 26, 2015
747
748
749
750
751
752
753
754
755
756
757
758
kmp = reqbuf->pos;
buf_append_tlv(reqbuf, 6, 0, NULL); /* TLV group 6 */
group = reqbuf->pos;
buf_append_tlv_be32(reqbuf, 2, vpninfo->ip_info.mtu);
if (buf_error(reqbuf)) {
vpn_progress(vpninfo, PRG_ERR,
_("Error creating oNCP negotiation request\n"));
ret = buf_error(reqbuf);
goto out;
}
put_len32(reqbuf, group);
put_len16(reqbuf, kmp);
Jan 26, 2015
Jan 26, 2015
759
Sep 10, 2016
Sep 10, 2016
760
#ifdef HAVE_ESP
May 19, 2019
May 19, 2019
761
if (!openconnect_setup_esp_keys(vpninfo, 1)) {
Jan 26, 2015
Jan 26, 2015
762
struct esp *esp = &vpninfo->esp_in[vpninfo->current_esp_in];
Jan 26, 2015
Jan 26, 2015
763
764
765
766
/* Since we'll want to do this in the oncp_mainloop too, where it's easier
* *not* to have an oc_text_buf and build it up manually, and since it's
* all fixed size and fairly simple anyway, just hard-code the packet */
buf_append_bytes(reqbuf, esp_kmp_hdr, sizeof(esp_kmp_hdr));
Jan 26, 2015
Jan 26, 2015
767
buf_append_bytes(reqbuf, &esp->spi, sizeof(esp->spi));
Jan 26, 2015
Jan 26, 2015
768
buf_append_bytes(reqbuf, esp_kmp_part2, sizeof(esp_kmp_part2));
May 15, 2017
May 15, 2017
769
770
buf_append_bytes(reqbuf, &esp->enc_key, vpninfo->enc_key_len);
buf_append_bytes(reqbuf, &esp->hmac_key, 0x40 - vpninfo->enc_key_len);
Jan 26, 2015
Jan 26, 2015
771
772
773
774
775
776
if (buf_error(reqbuf)) {
vpn_progress(vpninfo, PRG_ERR,
_("Error negotiating ESP keys\n"));
ret = buf_error(reqbuf);
goto out;
}
Jan 26, 2015
Jan 26, 2015
777
}
Jan 26, 2015
Jan 26, 2015
778
#endif
Jan 26, 2015
Jan 26, 2015
779
/* Length at the start of the packet is little-endian */
Jan 26, 2015
Jan 26, 2015
780
store_le16(reqbuf->data, reqbuf->pos - 2);
Jan 26, 2015
Jan 26, 2015
781
Aug 14, 2017
Aug 14, 2017
782
783
vpn_progress(vpninfo, PRG_DEBUG, _("oNCP negotiation request outgoing:\n"));
dump_buf_hex(vpninfo, PRG_DEBUG, '>', (void *)reqbuf->data, reqbuf->pos);
Jan 26, 2015
Jan 26, 2015
784
ret = vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos);
Jan 26, 2015
Jan 26, 2015
785
786
787
788
789
790
791
if (ret == reqbuf->pos)
ret = 0;
else if (ret >= 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Short write in oNCP negotiation\n"));
ret = -EIO;
}
Jan 26, 2015
Jan 26, 2015
792
793
794
out:
if (ret)
openconnect_close_https(vpninfo, 0);
Jan 26, 2015
Jan 26, 2015
795
796
797
798
799
else {
monitor_fd_new(vpninfo, ssl);
monitor_read_fd(vpninfo, ssl);
monitor_except_fd(vpninfo, ssl);
}
Jan 26, 2015
Jan 26, 2015
800
buf_free(reqbuf);
Mar 14, 2015
Mar 14, 2015
801
Mar 14, 2015
Mar 14, 2015
802
803
804
805
vpninfo->oncp_rec_size = 0;
free(vpninfo->cstp_pkt);
vpninfo->cstp_pkt = NULL;
Jan 26, 2015
Jan 26, 2015
806
return ret;
Jan 26, 2015
Jan 26, 2015
807
808
}
Jan 26, 2015
Jan 26, 2015
809
810
static int oncp_receive_espkeys(struct openconnect_info *vpninfo, int len)
{
Sep 10, 2016
Sep 10, 2016
811
#ifdef HAVE_ESP
Jan 26, 2015
Jan 26, 2015
812
813
int ret;
Mar 14, 2015
Mar 14, 2015
814
ret = parse_conf_pkt(vpninfo, vpninfo->cstp_pkt->oncp.kmp, len + 20, 301);
Oct 9, 2019
Oct 9, 2019
815
816
817
if (!ret)
ret = openconnect_setup_esp_keys(vpninfo, 1);
if (!ret) {
Jan 26, 2015
Jan 26, 2015
818
struct esp *esp = &vpninfo->esp_in[vpninfo->current_esp_in];
Mar 14, 2015
Mar 14, 2015
819
unsigned char *p = vpninfo->cstp_pkt->oncp.kmp;
Jan 26, 2015
Jan 26, 2015
820
821
822
memcpy(p, esp_kmp_hdr, sizeof(esp_kmp_hdr));
p += sizeof(esp_kmp_hdr);
Jan 26, 2015
Jan 26, 2015
823
824
memcpy(p, &esp->spi, sizeof(esp->spi));
p += sizeof(esp->spi);
Jan 26, 2015
Jan 26, 2015
825
826
memcpy(p, esp_kmp_part2, sizeof(esp_kmp_part2));
p += sizeof(esp_kmp_part2);
May 15, 2017
May 15, 2017
827
828
829
memcpy(p, esp->enc_key, vpninfo->enc_key_len);
memcpy(p+vpninfo->enc_key_len, esp->hmac_key, 0x40 - vpninfo->enc_key_len);
p += 0x40;
Jan 26, 2015
Jan 26, 2015
830
vpninfo->cstp_pkt->len = p - vpninfo->cstp_pkt->data;
Mar 14, 2015
Mar 14, 2015
831
832
store_le16(vpninfo->cstp_pkt->oncp.rec,
(p - vpninfo->cstp_pkt->oncp.kmp));
Jan 26, 2015
Jan 26, 2015
833
Jan 26, 2015
Jan 26, 2015
834
835
836
queue_packet(&vpninfo->oncp_control_queue, vpninfo->cstp_pkt);
vpninfo->cstp_pkt = NULL;
Jan 26, 2015
Jan 26, 2015
837
print_esp_keys(vpninfo, _("new incoming"), esp);
Jan 26, 2015
Jan 26, 2015
838
839
840
print_esp_keys(vpninfo, _("new outgoing"), &vpninfo->esp_out);
}
return ret;
Jan 26, 2015
Jan 26, 2015
841
842
843
844
845
#else
vpn_progress(vpninfo, PRG_DEBUG,
_("Ignoring ESP keys since ESP support not available in this build\n"));
return 0;
#endif
Jan 26, 2015
Jan 26, 2015
846
}
Jan 26, 2015
Jan 26, 2015
847
Mar 14, 2015
Mar 14, 2015
848
849
static int oncp_record_read(struct openconnect_info *vpninfo, void *buf, int len)
Jan 26, 2015
Jan 26, 2015
850
851
852
{
int ret;
Mar 14, 2015
Mar 14, 2015
853
854
if (!vpninfo->oncp_rec_size) {
unsigned char lenbuf[2];
Jan 26, 2015
Jan 26, 2015
855
Mar 14, 2015
Mar 14, 2015
856
857
858
859
860
861
ret = ssl_nonblock_read(vpninfo, lenbuf, 2);
if (ret <= 0)
return ret;
if (ret == 1) {
/* Surely at least *this* never happens? The two length bytes
* of the oNCP record being split across multiple SSL records */
Jan 26, 2015
Jan 26, 2015
862
vpn_progress(vpninfo, PRG_ERR,
Mar 14, 2015
Mar 14, 2015
863
864
_("Read only 1 byte of oNCP length field\n"));
return -EIO;
Jan 26, 2015
Jan 26, 2015
865
}
Mar 14, 2015
Mar 14, 2015
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
vpninfo->oncp_rec_size = load_le16(lenbuf);
if (!vpninfo->oncp_rec_size) {
ret = ssl_nonblock_read(vpninfo, lenbuf, 1);
if (ret == 1) {
if (lenbuf[0] == 1) {
vpn_progress(vpninfo, PRG_ERR,
_("Server terminated connection (session expired)\n"));
vpninfo->quit_reason = "VPN session expired";
} else {
vpn_progress(vpninfo, PRG_ERR,
_("Server terminated connection (reason: %d)\n"),
lenbuf[0]);
vpninfo->quit_reason = "Server terminated connection";
}
} else {
Jan 26, 2015
Jan 26, 2015
881
vpn_progress(vpninfo, PRG_ERR,
Mar 14, 2015
Mar 14, 2015
882
883
_("Server sent zero-length oNCP record\n"));
vpninfo->quit_reason = "Zero-length oNCP record";
Jan 26, 2015
Jan 26, 2015
884
}
Mar 14, 2015
Mar 14, 2015
885
return -EIO;
Jan 26, 2015
Jan 26, 2015
886
887
}
}
Mar 14, 2015
Mar 14, 2015
888
889
890
891
892
893
894
if (len > vpninfo->oncp_rec_size)
len = vpninfo->oncp_rec_size;
ret = ssl_nonblock_read(vpninfo, buf, len);
if (ret > 0)
vpninfo->oncp_rec_size -= ret;
return ret;
Jan 26, 2015
Jan 26, 2015
895
896
}
Apr 15, 2019
Apr 15, 2019
897
int oncp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
Jan 26, 2015
Jan 26, 2015
898
{
Jan 26, 2015
Jan 26, 2015
899
900
901
902
903
904
905
906
907
908
909
910
int ret;
int work_done = 0;
if (vpninfo->ssl_fd == -1)
goto do_reconnect;
/* FIXME: The poll() handling here is fairly simplistic. Actually,
if the SSL connection stalls it could return a WANT_WRITE error
on _either_ of the SSL_read() or SSL_write() calls. In that case,
we should probably remove POLLIN from the events we're looking for,
and add POLLOUT. As it is, though, it'll just chew CPU time in that
fairly unlikely situation, until the write backlog clears. */
Apr 15, 2019
Apr 15, 2019
911
while (readable) {
Mar 14, 2015
Mar 14, 2015
912
int len, kmp, kmplen, iplen;
May 14, 2017
May 14, 2017
913
914
915
916
/* Some servers send us packets that are larger than
negitiated MTU. We reserve some estra space to
handle that */
int receive_mtu = MAX(16384, vpninfo->ip_info.mtu);
Jan 26, 2015
Jan 26, 2015
917
May 14, 2017
May 14, 2017
918
len = receive_mtu + vpninfo->pkt_trailer;
Jan 26, 2015
Jan 26, 2015
919
920
921
922
923
924
if (!vpninfo->cstp_pkt) {
vpninfo->cstp_pkt = malloc(sizeof(struct pkt) + len);
if (!vpninfo->cstp_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
}
Mar 14, 2015
Mar 14, 2015
925
vpninfo->cstp_pkt->len = 0;
Jan 26, 2015
Jan 26, 2015
926
927
}
Jan 26, 2015
Jan 26, 2015
928
/*
Mar 14, 2015
Mar 14, 2015
929
930
931
932
933
934
935
* This protocol is horrid. There are encapsulations within
* encapsulations within encapsulations. Some of them entirely
* gratuitous.
*
* First there's the SSL records which are a natural part of
* using TLS as a transport. They appear to make no use of the
* packetisation which these provide.
Jan 26, 2015
Jan 26, 2015
936
*
Mar 14, 2015
Mar 14, 2015
937
938
939
940
941
942
* Then within the TLS data stream there are "records" preceded
* by a 16-bit little-endian length. It's not clear what these
* records represent; they appear to be entirely gratuitous and
* just need to be discarded. A record boundary sometimes falls
* right in the middle of a data packet; there's no apparent
* logic to it.
Jan 26, 2015
Jan 26, 2015
943
*
Mar 14, 2015
Mar 14, 2015
944
945
946
947
948
949
950
951
952
* Then there are the KMP packets themselves, each of which has
* a length field of its own. There can be multiple KMP packets
* in each of the above-mention "records", and as noted there
* even be *partial* KMP packets in each record.
*
* Finally, a KMP data packet may actually contain multiple IP
* packets, which need to be split apart by using the length
* field in the IP header. This is Legacy IP only, never IPv6
* for the Network Connect protocol.
Jan 26, 2015
Jan 26, 2015
953
*/
Jan 26, 2015
Jan 26, 2015
954
Mar 14, 2015
Mar 14, 2015
955
956
957
958
/* Until we pass it up the stack, we use cstp_pkt->len to show
* the amount of data received *including* the KMP header. */
len = oncp_record_read(vpninfo,
vpninfo->cstp_pkt->oncp.kmp + vpninfo->cstp_pkt->len,
May 14, 2017
May 14, 2017
959
receive_mtu + 20 - vpninfo->cstp_pkt->len);
Jan 26, 2015
Jan 26, 2015
960
961
if (!len)
break;
Mar 14, 2015
Mar 14, 2015
962
963
964
else if (len < 0) {
if (vpninfo->quit_reason)
return len;
Jan 26, 2015
Jan 26, 2015
965
goto do_reconnect;
Jan 26, 2015
Jan 26, 2015
966
}
Mar 14, 2015
Mar 14, 2015
967
968
969
970
vpninfo->cstp_pkt->len += len;
vpninfo->ssl_times.last_rx = time(NULL);
if (vpninfo->cstp_pkt->len < 20)
continue;
Jan 26, 2015
Jan 26, 2015
971
Mar 14, 2015
Mar 14, 2015
972
973
next_kmp:
/* Now we have a KMP header. It might already have been there */
Mar 14, 2015
Mar 14, 2015
974
975
kmp = load_be16(vpninfo->cstp_pkt->oncp.kmp + 6);
kmplen = load_be16(vpninfo->cstp_pkt->oncp.kmp + 18);
Mar 14, 2015
Mar 14, 2015
976
977
978
979
980
981
if (len == vpninfo->cstp_pkt->len)
vpn_progress(vpninfo, PRG_DEBUG, _("Incoming KMP message %d of size %d (got %d)\n"),
kmp, kmplen, vpninfo->cstp_pkt->len - 20);
else
vpn_progress(vpninfo, PRG_DEBUG, _("Continuing to process KMP message %d now size %d (got %d)\n"),
kmp, kmplen, vpninfo->cstp_pkt->len - 20);
Jan 26, 2015
Jan 26, 2015
982
983
984
switch (kmp) {
case 300:
Mar 14, 2015
Mar 14, 2015
985
986
987
988
989
990
991
992
993
next_ip:
/* Need at least 6 bytes of payload to check the IP packet length */
if (vpninfo->cstp_pkt->len < 26)
continue;
switch(vpninfo->cstp_pkt->data[0] >> 4) {
case 4:
iplen = load_be16(vpninfo->cstp_pkt->data + 2);
break;
case 6:
Dec 13, 2016
Dec 13, 2016
994
iplen = load_be16(vpninfo->cstp_pkt->data + 4) + 40;
Mar 14, 2015
Mar 14, 2015
995
996
997
998
999
1000
break;
default:
badiplen:
vpn_progress(vpninfo, PRG_ERR,
_("Unrecognised data packet\n"));
goto unknown_pkt;