-
Notifications
You must be signed in to change notification settings - Fork 0
/
TlsTester.java
370 lines (309 loc) · 14.4 KB
/
TlsTester.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
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
/*
* Copyright Tero Saarni
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fi.protonode.reloadingkeystore;
import java.io.Closeable;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.KeyStoreBuilderParameters;
import javax.net.ssl.SNIHostName;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.protonode.certy.Credential;
public class TlsTester {
private static final Logger log = LoggerFactory.getLogger(TlsTester.class);
private TlsTester() {
}
/**
* Server that accepts client connections, executes TLS handshake and then closes the client connection.
*/
static public class Server implements Runnable, Closeable {
private static final Logger log = LoggerFactory.getLogger(Server.class);
private final String host = "localhost";
private final int preferredPort = 18040; // Used port may be different if preferred port is not free.
private ExecutorService executor = Executors.newSingleThreadExecutor();
private final SSLContext ctx = SSLContext.getInstance("TLS");
private SSLServerSocket socket;
private boolean clientAuthentication;
CompletableFuture<Certificate[]> clientCertificates;
Server(KeyManager[] kms, TrustManager[] tms)
throws NoSuchAlgorithmException, KeyManagementException, IOException {
// Initialize server socket.
log.debug("Creating server socket (KeyStore={}, TrustStore={})",
kms != null ? "present" : "none",
tms != null ? "present" : "none");
ctx.init(kms, tms, null);
SSLServerSocketFactory ssf = ctx.getServerSocketFactory();
// Prefer given port, if that fails use any free port.
try {
log.debug("Binding server socket to {}:{}", host, preferredPort);
socket = (SSLServerSocket) ssf.createServerSocket(preferredPort, 1, InetAddress.getByName(host));
} catch (IOException e) {
log.debug("Binding server socket to {}:{} failed: {}", host, preferredPort, e.getMessage());
socket = (SSLServerSocket) ssf.createServerSocket(0, 1, InetAddress.getByName(host));
log.debug("Bound random free server port {}:{}", host, socket.getLocalPort());
}
// Since TLSv1.3 uses encrypted handshake, using Wireshark becomes bit more tricky. Therefore use TLSv1.2 in
// the tests, for observability reasons.
socket.setEnabledProtocols(new String[] { "TLSv1.2" });
// Try to avoid getting stuck in TIME_WAIT in test cases.
socket.setReuseAddress(true);
// Enable client authentication if TrustManager(s) are given.
clientAuth(tms != null);
// Future for passing client credentials back to main thread.
clientCertificates = new CompletableFuture<>();
// Start the server.
executor.execute(this);
}
public Server protocols(String[] protocols) {
log.debug("Setting protocols: {}", (Object) protocols);
socket.setEnabledProtocols(protocols);
return this;
}
public Server ciphers(String[] suites) {
log.debug("Setting ciphers: {}", (Object) suites);
socket.setEnabledCipherSuites(suites);
return this;
}
public Server clientAuth(boolean enable) {
log.debug("{} client authentication", enable ? "Enabling" : "Disabling");
socket.setWantClientAuth(enable);
socket.setNeedClientAuth(enable);
clientAuthentication = enable;
return this;
}
public Server swapKeyManagerAndTrustManager(KeyManager[] kms, TrustManager[] tms) throws KeyManagementException {
log.debug("Swapping TrustManager");
// Protect ctx: the method is called from main thread and used also by the server thread.
synchronized (ctx) {
ctx.init(kms, tms, null);
}
return this;
}
@Override
public void close() {
try {
socket.close();
executor.shutdown();
} catch (IOException e) {
log.error("Received exception:", e);
e.printStackTrace();
}
}
@Override
public void run() {
// Wait for client to connect.
while (true) {
log.debug("Listening for client to connect...");
try (SSLSocket client = (SSLSocket) socket.accept()) {
// Protect ctx because we can swap KeyManager and TrustManager in main thread.
synchronized (ctx) {
// Execute TLS handshake.
log.debug("Client connected: executing TLS handshake");
client.startHandshake();
// Pass the client credentials to main thread if client authentication was enabled.
if (clientAuthentication) {
SSLSession sess = client.getSession();
clientCertificates.complete(sess.getPeerCertificates());
}
}
} catch (SocketException e) {
// Socket closed.
log.debug("Socket: {}", e.getMessage());
break;
} catch (Exception e) {
log.error("Received exception:", e);
e.printStackTrace();
}
}
log.debug("Server exiting");
}
public int getPort() {
return socket.getLocalPort();
}
public String getHost() {
return host;
}
/**
* Returns the client certificates that were used to connect the server. If client authentication was not used,
* times out in two seconds and raises an exception. Can be called in main thread.
*/
public Certificate[] getClientCertificates()
throws InterruptedException, ExecutionException, SSLPeerUnverifiedException {
try {
return clientCertificates.get(2, TimeUnit.SECONDS);
} catch (TimeoutException e) {
throw new SSLPeerUnverifiedException("Did not receive client certificate");
}
}
}
/**
* Creates server with server authentication only.
*/
public static Server serverWithServerAuth(KeyManager[] kms)
throws KeyManagementException, NoSuchAlgorithmException, IOException {
return new Server(kms, null);
}
/**
* Creates server with mutual authentication.
*/
public static Server serverWithMutualAuth(KeyManager[] kms, TrustManager[] tms)
throws KeyManagementException, NoSuchAlgorithmException, IOException {
return new Server(kms, tms);
}
/**
* Client that establishes connection, executes TLS handshake and then closes the connection.
*/
static public class Client {
private static final Logger log = LoggerFactory.getLogger(Client.class);
private final SSLContext ctx = SSLContext.getInstance("TLS");
SSLSocket socket;
public Client(KeyManager[] kms, TrustManager[] tms)
throws NoSuchAlgorithmException, KeyManagementException, IOException {
// Initialize client socket.
log.debug("Creating client socket (KeyStore={}, TrustStore={})",
kms != null ? "present" : "none",
tms != null ? "present" : "none");
ctx.init(kms, tms, null);
SSLSocketFactory sf = ctx.getSocketFactory();
socket = (SSLSocket) sf.createSocket();
}
public Client serverName(String serverName) {
log.debug("Setting SNI servername: {}", serverName);
SSLParameters params = socket.getSSLParameters();
SNIHostName sni = new SNIHostName(serverName);
params.setServerNames(Arrays.asList(sni));
socket.setSSLParameters(params);
return this;
}
public Client ciphers(String[] suites) {
log.debug("Setting ciphers: {}", (Object) suites);
socket.setEnabledCipherSuites(suites);
return this;
}
public Client protocols(String[] protocols) {
log.debug("protocols ciphers: {}", (Object) protocols);
socket.setEnabledProtocols(protocols);
return this;
}
public Client connect(Server server) throws IOException {
return connect(server.getHost(), server.getPort());
}
public Client connect(String host, int port) throws IOException {
log.debug("Connecting to server {}:{}...", host, port);
socket.connect(new InetSocketAddress(host, port), 2000 /* msec */);
log.debug("Connected to server: executing TLS handshake");
socket.startHandshake();
return this;
}
public Certificate[] getServerCertificate() throws SSLPeerUnverifiedException {
return socket.getSession().getPeerCertificates();
}
}
/**
* Creates client that connects to the server using TLS server authentication only.
*/
public static Client connect(Server server, TrustManager[] tms)
throws UnknownHostException, IOException, KeyManagementException, NoSuchAlgorithmException {
return new Client(null, tms).connect(server);
}
/**
* Creates client that connects to the server using mutual TLS authentication.
*/
public static Client connect(Server server, KeyManager[] kms, TrustManager[] tms)
throws KeyManagementException, UnknownHostException, NoSuchAlgorithmException, IOException {
return new Client(kms, tms).connect(server);
}
/**
* Creates client that connects to the server using server authentication only, sends server name in SNI extension.
*/
public static Client connectWithSni(Server server, String serverName, TrustManager[] tms)
throws KeyManagementException, UnknownHostException, NoSuchAlgorithmException, IOException {
return new Client(null, tms).serverName(serverName).connect(server);
}
public static KeyManagerFactory createKeyManagerFactory(Path tempDir, Credential... credentials)
throws KeyStoreException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException,
IOException, InvalidAlgorithmParameterException, NoSuchProviderException {
// Create empty KeyStore.
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(null, null);
// Add given certificates and private keys as KeyEntries.
for (Credential c : credentials) {
ks.setKeyEntry(c.getX509Certificate().getSubjectX500Principal().toString(),
c.getPrivateKey(), "".toCharArray(), c.getCertificates());
}
// Store keystore to disk.
Path ksPath = tempDir.resolve(ks.toString());
log.debug("Writing keystore: {}", ksPath);
ks.store(Files.newOutputStream(ksPath), "".toCharArray());
// Load the keystore from disk with ReloadingKeyStore and construct KeyManagerFactory for it.
KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
kmf.init(
new KeyStoreBuilderParameters(ReloadingKeyStore.Builder.fromKeyStoreFile("PKCS12", ksPath, "")));
return kmf;
}
public static TrustManagerFactory createTrustManagerFactory(Path tempDir, Credential... credentials)
throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
NoSuchProviderException {
// Create empty KeyStore.
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(null, null);
// Add given trusted certificates as CertificateEntries.
for (Credential c : credentials) {
ks.setCertificateEntry(c.getX509Certificate().getSubjectX500Principal().toString(), c.getCertificate());
}
// Store keystore to disk.
Path ksPath = tempDir.resolve(ks.toString());
log.debug("Writing truststore: {}", ksPath);
ks.store(Files.newOutputStream(ksPath), "".toCharArray());
// Load the keystore from disk with ReloadingKeyStore and construct TrustManagerFactory for it.
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ReloadingKeyStore.Builder.fromKeyStoreFile("PKCS12", ksPath, "").getKeyStore());
return tmf;
}
}