/
pulse.c
2929 lines (2536 loc) · 85.1 KB
/
pulse.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
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
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2019 David Woodhouse.
*
* 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 <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>
#include <sys/types.h>
#include "openconnect-internal.h"
#define VENDOR_JUNIPER 0xa4c
#define VENDOR_JUNIPER2 0x583
#define VENDOR_TCG 0x5597
#define IFT_VERSION_REQUEST 1
#define IFT_VERSION_RESPONSE 2
#define IFT_CLIENT_AUTH_REQUEST 3
#define IFT_CLIENT_AUTH_SELECTION 4
#define IFT_CLIENT_AUTH_CHALLENGE 5
#define IFT_CLIENT_AUTH_RESPONSE 6
#define IFT_CLIENT_AUTH_SUCCESS 7
/* IF-T/TLS v1 authentication messages all start
* with the Auth Type Vendor (Juniper) + Type (1) */
#define JUNIPER_1 ((VENDOR_JUNIPER << 8) | 1)
#define AVP_VENDOR 0x80
#define AVP_MANDATORY 0x40
#define EAP_REQUEST 1
#define EAP_RESPONSE 2
#define EAP_SUCCESS 3
#define EAP_FAILURE 4
#define EAP_TYPE_IDENTITY 1
#define EAP_TYPE_GTC 6
#define EAP_TYPE_TLS 0x0d
#define EAP_TYPE_TTLS 0x15
#define EAP_TYPE_EXPANDED 0xfe
#define EXPANDED_JUNIPER ((EAP_TYPE_EXPANDED << 24) | VENDOR_JUNIPER)
#define AVP_CODE_EAP_MESSAGE 79
#if defined(OPENCONNECT_OPENSSL)
#define TTLS_SEND SSL_write
#define TTLS_RECV SSL_read
#elif defined(OPENCONNECT_GNUTLS)
#define TTLS_SEND gnutls_record_send
#define TTLS_RECV gnutls_record_recv
#endif
/* Flags for prompt handling during authentication, based on the contents of the 0xd73 AVP (qv). */
#define PROMPT_PRIMARY 1
#define PROMPT_USERNAME 2
#define PROMPT_PASSWORD 4
#define PROMPT_GTC_NEXT 0x10000
/* Request codes for the Juniper Expanded/2 auth requests. */
#define J2_PASSCHANGE 0x43
#define J2_PASSREQ 0x01
#define J2_PASSRETRY 0x81
#define J2_PASSFAIL 0xc5
/* Limit to TLS record size. */
#define TLS_RECORD_MAX (16384)
/* Outbound fragment size limit */
#define TTLS_MAXFRAG (8192)
#define TTLS_LENGTH (1<<7)
#define TTLS_MOREFRAGS (1<<6)
#define TTLS_START (1<<5)
static void buf_append_be16(struct oc_text_buf *buf, uint16_t val)
{
unsigned char b[2];
store_be16(b, val);
buf_append_bytes(buf, b, 2);
}
static void buf_append_be32(struct oc_text_buf *buf, uint32_t val)
{
unsigned char b[4];
store_be32(b, val);
buf_append_bytes(buf, b, 4);
}
static void buf_append_ift_hdr(struct oc_text_buf *buf, uint32_t vendor, uint32_t type)
{
uint32_t b[4];
store_be32(&b[0], vendor);
store_be32(&b[1], type);
b[2] = 0; /* Length will be filled in later. */
b[3] = 0;
buf_append_bytes(buf, b, 16);
}
/* Append EAP header, using VENDOR_JUNIPER and the given subtype if
* the main type is EAP_TYPE_EXPANDED */
static int buf_append_eap_hdr(struct oc_text_buf *buf, uint8_t code, uint8_t ident, uint8_t type,
uint32_t subtype)
{
unsigned char b[24];
int len_ofs = -1;
if (!buf_error(buf))
len_ofs = buf->pos;
b[0] = code;
b[1] = ident;
b[2] = b[3] = 0; /* Length is filled in later. */
if (type == EAP_TYPE_EXPANDED) {
store_be32(b + 4, EXPANDED_JUNIPER);
store_be32(b + 8, subtype);
buf_append_bytes(buf, b, 12);
} else {
b[4] = type;
buf_append_bytes(buf, b, 5);
}
return len_ofs;
}
/* For an IF-T/TLS auth frame containing the Juniper/1 Auth Type,
* the EAP header is at offset 0x14. Fill in the length field,
* based on the current length of the buf */
static void buf_fill_eap_len(struct oc_text_buf *buf, int ofs)
{
/* EAP length word is always at 0x16, and counts bytes from 0x14 */
if (ofs >= 0 && !buf_error(buf) && buf->pos >= ofs + 4)
store_be16(buf->data + ofs + 2, buf->pos - ofs);
}
static void buf_append_avp(struct oc_text_buf *buf, uint32_t type, const void *bytes, int len)
{
buf_append_be32(buf, type);
buf_append_be16(buf, 0x8000);
buf_append_be16(buf, len + 12);
buf_append_be32(buf, VENDOR_JUNIPER2);
buf_append_bytes(buf, bytes, len);
if (len & 3) {
uint32_t pad = 0;
buf_append_bytes(buf, &pad, 4 - ( len & 3 ));
}
}
static void buf_append_avp_string(struct oc_text_buf *buf, uint32_t type, const char *str)
{
buf_append_avp(buf, type, str, strlen(str));
}
static void buf_append_avp_be32(struct oc_text_buf *buf, uint32_t type, uint32_t val)
{
uint32_t val_be;
store_be32(&val_be, val);
buf_append_avp(buf, type, &val_be, sizeof(val_be));
}
static int valid_ift_success(unsigned char *bytes, int len)
{
if (len != 0x18 || (load_be32(bytes) & 0xffffff) != VENDOR_TCG ||
load_be32(bytes + 4) != IFT_CLIENT_AUTH_SUCCESS ||
load_be32(bytes + 8) != len ||
load_be32(bytes + 0x10) != JUNIPER_1 ||
bytes[0x14] != EAP_SUCCESS ||
load_be16(bytes + 0x16) != len - 0x14)
return 0;
return 1;
}
/* Check for a valid IF-T/TLS auth challenge of the Juniper/1 Auth Type */
static int valid_ift_auth(unsigned char *bytes, int len)
{
if (len < 0x14 || (load_be32(bytes) & 0xffffff) != VENDOR_TCG ||
load_be32(bytes + 4) != IFT_CLIENT_AUTH_CHALLENGE ||
load_be32(bytes + 8) != len ||
load_be32(bytes + 0x10) != JUNIPER_1)
return 0;
return 1;
}
static int valid_ift_auth_eap(unsigned char *bytes, int len)
{
/* Needs to be a valid IF-T/TLS auth challenge with the
* expect Auth Type, *and* the payload has to be a valid
* EAP request with correct length field. */
if (!valid_ift_auth(bytes, len) || len < 0x19 ||
bytes[0x14] != EAP_REQUEST ||
load_be16(bytes + 0x16) != len - 0x14)
return 0;
return 1;
}
static int valid_ift_auth_eap_exj1(unsigned char *bytes, int len)
{
/* Also needs to be the Expanded Juniper/1 EAP Type */
if (!valid_ift_auth_eap(bytes, len) || len < 0x20 ||
load_be32(bytes + 0x18) != EXPANDED_JUNIPER ||
load_be32(bytes + 0x1c) != 1)
return 0;
return 1;
}
/* 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, uint16_t type,
unsigned char *data, int attrlen)
{
struct oc_split_include *xc;
char buf[80];
int i;
switch (type) {
case 0x0001:
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 Legacy IP address %s\n"), buf);
vpninfo->ip_info.addr = add_option(vpninfo, "ipaddr", buf, -1);
break;
case 0x0002:
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);
vpninfo->ip_info.netmask = add_option(vpninfo, "netmask", buf, -1);
break;
case 0x0003:
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 0x0004:
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 0x0008:
if (attrlen != 17)
goto badlen;
if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to handle IPv6 address\n"));
return -EINVAL;
}
vpninfo->ip_info.addr6 = add_option(vpninfo, "ip6addr", buf, -1);
i = strlen(buf);
snprintf(buf + i, sizeof(buf) - i, "/%d", data[16]);
vpninfo->ip_info.netmask6 = add_option(vpninfo, "ip6netmask", buf, -1);
vpn_progress(vpninfo, PRG_DEBUG, _("Received internal IPv6 address %s\n"), buf);
break;
case 0x000a:
if (attrlen != 16)
goto badlen;
if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to handle IPv6 address\n"));
return -EINVAL;
}
for (i = 0; i < 3; i++) {
if (!vpninfo->ip_info.dns[i]) {
vpninfo->ip_info.dns[i] = add_option(vpninfo, "DNS", buf, -1);
break;
}
}
vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS server %s\n"), buf);
break;
case 0x000f:
if (attrlen != 17)
goto badlen;
if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to handle IPv6 address\n"));
return -EINVAL;
}
i = strlen(buf);
snprintf(buf + i, sizeof(buf) - i, "/%d", data[16]);
xc = malloc(sizeof(*xc));
if (xc) {
xc->route = add_option(vpninfo, "split-include6", buf, -1);
if (xc->route) {
xc->next = vpninfo->ip_info.split_includes;
vpninfo->ip_info.split_includes = xc;
} else
free(xc);
}
vpn_progress(vpninfo, PRG_DEBUG, _("Received IPv6 split include %s\n"), buf);
break;
case 0x0010:
if (attrlen != 17)
goto badlen;
if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to handle IPv6 address\n"));
return -EINVAL;
}
i = strlen(buf);
snprintf(buf + i, sizeof(buf) - i, "/%d", data[16]);
xc = malloc(sizeof(*xc));
if (xc) {
xc->route = add_option(vpninfo, "split-exclude6", buf, -1);
if (xc->route) {
xc->next = vpninfo->ip_info.split_excludes;
vpninfo->ip_info.split_excludes = xc;
} else
free(xc);
}
vpn_progress(vpninfo, PRG_DEBUG, _("Received IPv6 split exclude %s\n"), buf);
break;
case 0x4005:
if (attrlen != 4) {
badlen:
vpn_progress(vpninfo, PRG_ERR,
_("Unexpected length %d for attr 0x%x\n"),
attrlen, type);
return -EINVAL;
}
vpninfo->ip_info.mtu = load_be32(data);
vpn_progress(vpninfo, PRG_DEBUG,
_("Received MTU %d from server\n"),
vpninfo->ip_info.mtu);
break;
case 0x4006:
if (!attrlen)
goto badlen;
if (!data[attrlen-1])
attrlen--;
vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS search domain %.*s\n"),
attrlen, (char *)data);
vpninfo->ip_info.domain = add_option(vpninfo, "search", (char *)data, attrlen);
if (vpninfo->ip_info.domain) {
char *p = (char *)vpninfo->ip_info.domain;
while ((p = strchr(p, ',')))
*p = ' ';
}
break;
case 0x400b:
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;
case 0x4010: {
const char *enctype;
uint16_t val;
if (attrlen != 2)
goto badlen;
val = load_be16(data);
if (val == ENC_AES_128_CBC) {
enctype = "AES-128";
vpninfo->enc_key_len = 16;
} else if (val == ENC_AES_256_CBC) {
enctype = "AES-256";
vpninfo->enc_key_len = 32;
} else
enctype = "unknown";
vpn_progress(vpninfo, PRG_DEBUG, _("ESP encryption: 0x%04x (%s)\n"),
val, enctype);
vpninfo->esp_enc = val;
break;
}
case 0x4011: {
const char *mactype;
uint16_t val;
if (attrlen != 2)
goto badlen;
val = load_be16(data);
if (val == HMAC_MD5) {
mactype = "MD5";
vpninfo->hmac_key_len = 16;
} else if (val == HMAC_SHA1) {
mactype = "SHA1";
vpninfo->hmac_key_len = 20;
} else if (val == HMAC_SHA256) {
mactype = "SHA256";
vpninfo->hmac_key_len = 32;
} else
mactype = "unknown";
vpn_progress(vpninfo, PRG_DEBUG, _("ESP HMAC: 0x%04x (%s)\n"),
val, mactype);
vpninfo->esp_hmac = val;
break;
}
case 0x4012:
if (attrlen != 4)
goto badlen;
vpninfo->esp_lifetime_seconds = load_be32(data);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP key lifetime: %u seconds\n"),
vpninfo->esp_lifetime_seconds);
break;
case 0x4013:
if (attrlen != 4)
goto badlen;
vpninfo->esp_lifetime_bytes = load_be32(data);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP key lifetime: %u bytes\n"),
vpninfo->esp_lifetime_bytes);
break;
case 0x4014:
if (attrlen != 4)
goto badlen;
vpninfo->esp_replay_protect = load_be32(data);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP replay protection: %d\n"),
load_be32(data));
break;
case 0x4016:
if (attrlen != 2)
goto badlen;
i = load_be16(data);
udp_sockaddr(vpninfo, i);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP port: %d\n"), i);
break;
case 0x4017:
if (attrlen != 4)
goto badlen;
vpninfo->esp_ssl_fallback = load_be32(data);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP to SSL fallback: %u seconds\n"),
vpninfo->esp_ssl_fallback);
break;
case 0x401a:
if (attrlen != 1)
goto badlen;
/* Amusingly, this isn't enforced. It's client-only */
vpn_progress(vpninfo, PRG_DEBUG, _("ESP only: %d\n"),
data[0]);
break;
#if 0
case GRP_ATTR(7, 1):
if (attrlen != 4)
goto badlen;
memcpy(&vpninfo->esp_out.spi, data, 4);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP SPI (outbound): %x\n"),
load_be32(data));
break;
case GRP_ATTR(7, 2):
if (attrlen != 0x40)
goto badlen;
/* data contains enc_key and hmac_key concatenated */
memcpy(vpninfo->esp_out.enc_key, data, 0x40);
vpn_progress(vpninfo, PRG_DEBUG, _("%d bytes of ESP secrets\n"),
attrlen);
break;
#endif
/* 0x4022: disable proxy
0x400a: preserve proxy
0x4008: proxy (string)
0x4000: disconnect when routes changed
0x4015: tos copy
0x4001: tunnel routes take precedence
0x401f: tunnel routes with subnet access (also 4001 set)
0x4020: Enforce IPv4
0x4021: Enforce IPv6
0x401e: Server IPv6 address
0x000f: IPv6 netmask?
*/
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 attr 0x%x len %d:%s\n"),
type, attrlen, buf);
}
return 0;
}
static int recv_ift_packet(struct openconnect_info *vpninfo, void *buf, int len)
{
int ret = vpninfo->ssl_read(vpninfo, buf, len);
if (ret > 0 && vpninfo->dump_http_traffic) {
vpn_progress(vpninfo, PRG_TRACE,
_("Read %d bytes of IF-T/TLS record\n"), ret);
dump_buf_hex(vpninfo, PRG_TRACE, '<', buf, ret);
}
return ret;
}
static int send_ift_bytes(struct openconnect_info *vpninfo, void *bytes, int len)
{
int ret;
store_be32(((char *)bytes) + 12, vpninfo->ift_seq++);
dump_buf_hex(vpninfo, PRG_DEBUG, '>', (void *)bytes, len);
ret = vpninfo->ssl_write(vpninfo, bytes, len);
if (ret != len) {
if (ret >= 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Short write to IF-T/TLS\n"));
ret = -EIO;
}
return ret;
}
return 0;
}
static int send_ift_packet(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
{
if (buf_error(buf) || buf->pos < 16) {
vpn_progress(vpninfo, PRG_ERR,
_("Error creating IF-T packet\n"));
return buf_error(buf);
}
/* Fill in the length word in the header with the full length of the buffer.
* Also populate the sequence number. */
store_be32(buf->data + 8, buf->pos);
return send_ift_bytes(vpninfo, buf->data, buf->pos);
}
/* We create packets with IF-T/TLS headers prepended because that's the
* larger header. In the case where they need to be sent over EAP-TTLS,
* convert the header to the EAP-Message AVP instead. */
static int send_eap_packet(struct openconnect_info *vpninfo, void *ttls, struct oc_text_buf *buf)
{
int ret;
if (buf_error(buf) || buf->pos < 16) {
vpn_progress(vpninfo, PRG_ERR,
_("Error creating EAP packet\n"));
return buf_error(buf);
}
if (!ttls)
return send_ift_packet(vpninfo, buf);
/* AVP EAP-Message header */
store_be32(buf->data + 0x0c, AVP_CODE_EAP_MESSAGE);
store_be32(buf->data + 0x10, buf->pos - 0xc);
dump_buf_hex(vpninfo, PRG_DEBUG, '.', (void *)(buf->data + 0x0c), buf->pos - 0x0c);
ret = TTLS_SEND(ttls, buf->data + 0x0c, buf->pos - 0x0c);
if (ret != buf->pos - 0x0c)
return -EIO;
return 0;
}
/*
* Using the given buffer, receive and validate an EAP request of the
* Expanded Juniper/1 type, either natively over IF-T/TLS or by EAP-TTLS
* over IF-T/TLS. Return a pointer to the EAP header, with its length and
* type already validated.
*/
static void *recv_eap_packet(struct openconnect_info *vpninfo, void *ttls, void *buf, int len)
{
unsigned char *cbuf = buf;
int ret;
if (!ttls) {
ret = recv_ift_packet(vpninfo, buf, len);
if (ret < 0)
return NULL;
if (!valid_ift_auth_eap_exj1(buf, ret)) {
vpn_progress(vpninfo, PRG_ERR,
_("Unexpected IF-T/TLS authentication challenge:\n"));
dump_buf_hex(vpninfo, PRG_ERR, '<', (void *)buf, ret);
return NULL;
}
return cbuf + 0x14;
} else {
ret = TTLS_RECV(ttls, buf, len);
if (ret <= 8)
return NULL;
if (/* EAP-Message AVP */
load_be32(cbuf) != AVP_CODE_EAP_MESSAGE ||
/* Ignore the mandatory bit */
(load_be32(cbuf+0x04) & ~0x40000000) != ret ||
cbuf[0x08] != EAP_REQUEST ||
load_be16(cbuf+0x0a) != ret - 8 ||
load_be32(cbuf+0x0c) != EXPANDED_JUNIPER ||
load_be32(cbuf+0x10) != 1) {
vpn_progress(vpninfo, PRG_ERR,
_("Unexpected EAP-TTLS payload:\n"));
dump_buf_hex(vpninfo, PRG_ERR, '<', buf, ret);
return NULL;
}
return cbuf + 0x08;
}
}
static void dump_avp(struct openconnect_info *vpninfo, uint8_t flags,
uint32_t vendor, uint32_t code, void *p, int len)
{
struct oc_text_buf *buf = buf_alloc();
const char *pretty;
int i;
for (i = 0; i < len; i++)
if (!isprint( ((char *)p)[i] ))
break;
if (i == len) {
buf_append(buf, " '");
buf_append_bytes(buf, p, len);
buf_append(buf, "'");
} else {
for (i = 0; i < len; i++)
buf_append(buf, " %02x", ((unsigned char *)p)[i]);
}
if (buf_error(buf))
pretty = " <error>";
else
pretty = buf->data;
if (flags & AVP_VENDOR)
vpn_progress(vpninfo, PRG_TRACE, _("AVP 0x%x/0x%x:%s\n"), vendor, code, pretty);
else
vpn_progress(vpninfo, PRG_TRACE, _("AVP %d:%s\n"), code, pretty);
buf_free(buf);
}
/* RFC5281 §10 */
static int parse_avp(struct openconnect_info *vpninfo, void **pkt, int *pkt_len,
void **avp_out, int *avp_len, uint8_t *avp_flags,
uint32_t *avp_vendor, uint32_t *avp_code)
{
unsigned char *p = *pkt;
int l = *pkt_len;
uint32_t code, len, vendor = 0;
uint8_t flags;
if (l < 8)
return -EINVAL;
code = load_be32(p);
len = load_be32(p + 4) & 0xffffff;
flags = p[4];
if (len > l || len < 8)
return -EINVAL;
p += 8;
l -= 8;
len -= 8;
/* Vendor field is optional. */
if (flags & AVP_VENDOR) {
if (l < 4)
return -EINVAL;
vendor = load_be32(p);
p += 4;
l -= 4;
len -= 4;
}
*avp_vendor = vendor;
*avp_flags = flags;
*avp_code = code;
*avp_out = p;
*avp_len = len;
/* Now set up packet pointer and length for next AVP,
* aligned to 4 octets (if they exist in the packet) */
len = (len + 3) & ~3;
if (len > l)
len = l;
*pkt = p + len;
*pkt_len = l - len;
return 0;
}
static int pulse_request_realm_entry(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf)
{
struct oc_auth_form f;
struct oc_form_opt o;
int ret;
memset(&f, 0, sizeof(f));
memset(&o, 0, sizeof(o));
f.auth_id = (char *)"pulse_realm_entry";
f.opts = &o;
f.message = _("Enter Pulse user realm:");
o.next = NULL;
o.type = OC_FORM_OPT_TEXT;
o.name = (char *)"realm";
o.label = (char *)_("Realm:");
ret = process_auth_form(vpninfo, &f);
if (ret)
return ret;
if (o._value) {
buf_append_avp_string(reqbuf, 0xd50, o._value);
free_pass(&o._value);
return 0;
}
return -EINVAL;
}
static int pulse_request_realm_choice(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf,
int realms, unsigned char *eap)
{
uint8_t avp_flags;
uint32_t avp_code;
uint32_t avp_vendor;
int avp_len;
void *avp_p;
struct oc_auth_form f;
struct oc_form_opt_select o;
int i = 0, ret;
void *p;
int l;
l = load_be16(eap + 2) - 0x0c; /* Already validated */
p = eap + 0x0c;
memset(&f, 0, sizeof(f));
memset(&o, 0, sizeof(o));
f.auth_id = (char *)"pulse_realm_choice";
f.opts = &o.form;
f.authgroup_opt = &o;
f.authgroup_selection = 1;
f.message = _("Choose Pulse user realm:");
o.form.next = NULL;
o.form.type = OC_FORM_OPT_SELECT;
o.form.name = (char *)"realm_choice";
o.form.label = (char *)_("Realm:");
o.nr_choices = realms;
o.choices = calloc(realms, sizeof(*o.choices));
if (!o.choices)
return -ENOMEM;
while (l) {
if (parse_avp(vpninfo, &p, &l, &avp_p, &avp_len, &avp_flags,
&avp_vendor, &avp_code)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to parse AVP\n"));
ret = -EINVAL;
goto out;
}
if (avp_vendor != VENDOR_JUNIPER2 || avp_code != 0xd4e)
continue;
o.choices[i] = malloc(sizeof(struct oc_choice));
if (!o.choices[i]) {
ret = -ENOMEM;
goto out;
}
o.choices[i]->name = o.choices[i]->label = strndup(avp_p, avp_len);
if (!o.choices[i]->name) {
ret = -ENOMEM;
goto out;
}
i++;
}
/* We don't need to do anything on group changes. */
do {
ret = process_auth_form(vpninfo, &f);
} while (ret == OC_FORM_RESULT_NEWGROUP);
if (!ret)
buf_append_avp_string(reqbuf, 0xd50, o.form._value);
out:
if (o.choices) {
for (i = 0; i < realms; i++) {
if (o.choices[i]) {
free(o.choices[i]->name);
free(o.choices[i]);
}
}
free(o.choices);
}
return ret;
}
static int pulse_request_session_kill(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf,
int sessions, unsigned char *eap)
{
uint8_t avp_flags;
uint32_t avp_code;
uint32_t avp_vendor;
int avp_len, avp_len2;
void *avp_p, *avp_p2;
struct oc_auth_form f;
struct oc_form_opt_select o;
int i = 0, ret;
void *p;
int l;
struct oc_text_buf *form_msg = buf_alloc();
char tmbuf[80];
struct tm tm;
l = load_be16(eap + 2) - 0x0c; /* Already validated */
p = eap + 0x0c;
memset(&f, 0, sizeof(f));
memset(&o, 0, sizeof(o));
f.auth_id = (char *)"pulse_session_kill";
f.opts = &o.form;
buf_append(form_msg, _("Session limit reached. Choose session to kill:\n"));
o.form.next = NULL;
o.form.type = OC_FORM_OPT_SELECT;
o.form.name = (char *)"session_choice";
o.form.label = (char *)_("Session:");
o.nr_choices = sessions;
o.choices = calloc(sessions, sizeof(*o.choices));
if (!o.choices) {
ret = -ENOMEM;
goto out;
}
while (l) {
char *from = NULL;
time_t when = 0;
char *sessid = NULL;
if (parse_avp(vpninfo, &p, &l, &avp_p, &avp_len, &avp_flags,
&avp_vendor, &avp_code)) {
badlist:
free(from);
free(sessid);
vpn_progress(vpninfo, PRG_ERR,
_("Failed to parse session list\n"));
ret = -EINVAL;
goto out;
}
if (avp_vendor != VENDOR_JUNIPER2 || avp_code != 0xd65)
continue;
while (avp_len) {
if (parse_avp(vpninfo, &avp_p, &avp_len, &avp_p2, &avp_len2,
&avp_flags, &avp_vendor, &avp_code))
goto badlist;
dump_avp(vpninfo, avp_flags, avp_vendor, avp_code, avp_p2, avp_len2);
if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd66) {
free(sessid);
sessid = strndup(avp_p2, avp_len2);
} else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd67) {
free(from);
from = strndup(avp_p2, avp_len2);
} else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd68 &&
avp_len2 == 8) {
when = load_be32((char *)avp_p2 + 4);
if (sizeof(time_t) > 4)
when |= ((uint64_t)load_be32(avp_p2)) << 32;
}
}
if (!from || !sessid || !when)
goto badlist;
if (0
#ifdef HAVE_LOCALTIME_S
|| !localtime_s(&tm, &when)
#endif
#ifdef HAVE_LOCALTIME_R
|| localtime_r(&when, &tm)
#endif
) {
strftime(tmbuf, sizeof(tmbuf), "%a, %d %b %Y %H:%M:%S %Z", &tm);
} else
snprintf(tmbuf, sizeof(tmbuf), "@%lu", (unsigned long)when);
buf_append(form_msg, " - %s from %s at %s\n", sessid, from, tmbuf);
free(from);
from = NULL;
o.choices[i] = malloc(sizeof(struct oc_choice));
if (!o.choices[i]) {
free(sessid);
ret = -ENOMEM;
goto out;
}