/
LibTest.java
249 lines (213 loc) · 6.94 KB
/
LibTest.java
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
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2013 Kevin Cernekee <cernekee@gmail.com>
*
* 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.
*/
package com.example;
import java.io.*;
import java.util.*;
import org.infradead.libopenconnect.LibOpenConnect;
public final class LibTest {
private static void die(String msg) {
System.out.println(msg);
System.exit(1);
}
private static String getline() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String line = br.readLine();
return line;
} catch (IOException e) {
die("\nI/O error");
}
return "";
}
private static class TestLib extends LibOpenConnect {
@Override
public int onValidatePeerCert(String msg) {
System.out.println("cert warning: " + msg);
System.out.println("cert hash: " + getPeerCertHash());
System.out.println("cert details: " + getPeerCertDetails());
System.out.println("Internal consistency check: " +
(checkPeerCertHash(getPeerCertHash()) == 0 ? "OK" : "FAIL"));
byte der[] = getPeerCertDER();
System.out.println("DER is " + der.length + " bytes long");
System.out.print("\nAccept this certificate? [n] ");
String s = getline();
if (s.startsWith("y") || s.startsWith("Y")) {
return 0;
} else {
return -1;
}
}
@Override
public int onWriteNewConfig(byte[] buf) {
System.out.println("NEW_CONFIG: " + buf.length + " bytes");
return 0;
}
@Override
public void onProtectSocket(int fd) {
System.out.println("PROTECT_FD: " + fd);
}
private void printChoices(FormOpt fo) {
for (FormChoice fc : fo.choices) {
System.out.println("--->FormChoice: ");
System.out.println(" +-name: " + fc.name);
System.out.println(" +-label: " + fc.label);
System.out.println(" +-authType: " + fc.authType);
System.out.println(" +-overrideName: " + fc.overrideName);
System.out.println(" +-overrideLabel: " + fc.overrideLabel);
}
}
private String authgroup;
private boolean lastFormEmpty;
@Override
public int onProcessAuthForm(LibOpenConnect.AuthForm authForm) {
boolean empty = true;
System.out.println("\nCSTP Cipher: " + getCSTPCipher());
System.out.println("\nAuthForm:");
System.out.println("+-banner: " + authForm.banner);
System.out.println("+-message: " + authForm.message);
System.out.println("+-error: " + authForm.error);
System.out.println("+-authID: " + authForm.authID);
System.out.println("+-method: " + authForm.method);
System.out.println("+-action: " + authForm.action);
if (authgroup == null && authForm.authgroupOpt != null) {
FormOpt fo = authForm.authgroupOpt;
printChoices(fo);
System.out.print("\n" + fo.label + " ");
String value = getline();
fo.value = value;
authgroup = value;
return OC_FORM_RESULT_NEWGROUP;
}
for (FormOpt fo : authForm.opts) {
System.out.println("->FormOpt: ");
System.out.println(" +-type: " + fo.type);
System.out.println(" +-name: " + fo.name);
System.out.println(" +-label: " + fo.label);
System.out.println(" +-flags: " + fo.flags);
if ((fo.flags & OC_FORM_OPT_IGNORE) != 0) {
continue;
}
if (fo.type == OC_FORM_OPT_SELECT) {
if (fo == authForm.authgroupOpt && authgroup != null) {
fo.value = authgroup;
continue;
}
printChoices(fo);
}
if (fo.type == OC_FORM_OPT_TEXT ||
fo.type == OC_FORM_OPT_PASSWORD ||
fo.type == OC_FORM_OPT_SELECT) {
System.out.print("\n" + fo.label + " ");
String value = getline();
fo.value = value;
empty = false;
}
}
System.out.println("");
if (lastFormEmpty && empty) {
return OC_FORM_RESULT_CANCELLED;
}
lastFormEmpty = empty;
return OC_FORM_RESULT_OK;
}
@Override
public void onProgress(int level, String msg) {
switch (level) {
case LibOpenConnect.PRG_TRACE:
System.out.print("TRACE: " + msg);
break;
case LibOpenConnect.PRG_DEBUG:
System.out.print("DEBUG: " + msg);
break;
case LibOpenConnect.PRG_INFO:
System.out.print("INFO: " + msg);
break;
case LibOpenConnect.PRG_ERR:
System.out.print("ERROR: " + msg);
break;
}
}
@Override
public void onSetupTun() {
System.out.println("SETUP_TUN");
if (setupTunDevice("/etc/vpnc/vpnc-script", null) != 0 &&
setupTunScript("ocproxy") != 0)
die("Error setting up tunnel");
}
}
private static void printList(String pfx, List<String> ss) {
System.out.print(pfx + ":");
if (ss.size() == 0) {
System.out.println(" <empty>");
return;
}
for (String s : ss) {
System.out.print(" " + s);
}
System.out.println("");
}
private static void printIPInfo(LibOpenConnect.IPInfo ip) {
System.out.println("\nIPInfo:");
System.out.println("+-IPv4: " + ip.addr + " / " + ip.netmask);
System.out.println("+-IPv6: " + ip.addr6 + " / " + ip.netmask6);
System.out.println("+-Domain: " + ip.domain);
System.out.println("+-proxy.pac: " + ip.proxyPac);
System.out.println("+-Gateway IP: " + ip.gatewayAddr);
System.out.println("+-MTU: " + ip.MTU);
printList("+-DNS", ip.DNS);
printList("+-NBNS", ip.NBNS);
printList("+-Split DNS", ip.splitDNS);
printList("+-Split includes", ip.splitIncludes);
printList("+-Split excludes", ip.splitExcludes);
System.out.println("");
}
public static void main(String argv[]) {
System.loadLibrary("openconnect-wrapper");
LibOpenConnect lib = new TestLib();
if (argv.length != 1)
die("usage: LibTest <server_name>");
System.out.println("OpenConnect version: " + lib.getVersion());
System.out.println(" PKCS=" + lib.hasPKCS11Support() +
", TSS=" + lib.hasTSSBlobSupport() +
", STOKEN=" + lib.hasStokenSupport() +
", OATH=" + lib.hasOATHSupport() +
", YUBIOATH=" + lib.hasYubiOATHSupport());
lib.setReportedOS("win");
lib.setLogLevel(lib.PRG_DEBUG);
//lib.setTokenMode(LibOpenConnect.OC_TOKEN_MODE_STOKEN, null);
if (new File("csd.sh").exists()) {
lib.setCSDWrapper("csd.sh", null, null);
}
lib.parseURL(argv[0]);
lib.setSystemTrust(true);
int ret = lib.obtainCookie();
if (ret < 0)
die("obtainCookie() returned error");
else if (ret > 0)
die("Aborted by user");
String cookie = lib.getCookie();
if (cookie.length() > 40) {
System.out.println("Cookie: " + cookie.substring(0, 40) + "...");
} else {
System.out.println("Cookie: " + cookie);
}
if (lib.makeCSTPConnection() != 0)
die("Error establishing VPN link");
printIPInfo(lib.getIPInfo());
if (lib.setupDTLS(60) != 0)
die("Error setting up DTLS");
lib.mainloop(300, LibOpenConnect.RECONNECT_INTERVAL_MIN);
}
}