Skip to content

Commit 390fd75

Browse files
committed
Put TLS handshake logging for client/cert behind a switch
Signed-off-by: arjantijms <arjan.tijms@gmail.com>
1 parent 9838910 commit 390fd75

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

servlet/security-clientcert/src/test/java/org/javaee7/servlet/security/clientcert/SecureServletTest.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static java.math.BigInteger.ONE;
55
import static java.time.Instant.now;
66
import static java.time.temporal.ChronoUnit.DAYS;
7+
import static java.util.logging.Level.FINEST;
78
import static org.javaee7.ServerOperations.addCertificateToContainerTrustStore;
89
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
910
import static org.junit.Assert.assertTrue;
@@ -38,6 +39,9 @@
3839
import javax.net.ssl.TrustManager;
3940
import javax.net.ssl.X509TrustManager;
4041

42+
import org.apache.commons.logging.Log;
43+
import org.apache.commons.logging.LogFactory;
44+
import org.apache.commons.logging.impl.Jdk14Logger;
4145
import org.bouncycastle.asn1.x500.X500Name;
4246
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
4347
import org.bouncycastle.cert.X509v3CertificateBuilder;
@@ -82,7 +86,13 @@ public static WebArchive createDeployment() throws FileNotFoundException, IOExce
8286
Security.addProvider(provider);
8387

8488
// Enable to get detailed logging about the SSL handshake
85-
// System.setProperty("javax.net.debug", "ssl:handshake");
89+
90+
// For an explanation of the TLS handshake see: https://tls.ulfheim.net
91+
92+
if (System.getProperty("ssl.debug") != null) {
93+
enableSSLDebug();
94+
}
95+
8696

8797
System.out.println("################################################################");
8898

@@ -91,12 +101,14 @@ public static WebArchive createDeployment() throws FileNotFoundException, IOExce
91101
// Generate a Private/Public key pair for the client
92102
KeyPair clientKeyPair = generateRandomKeys();
93103

94-
// Create a certificate containing the public key and signed with the private key
104+
// Create a certificate containing the client public key and signed with the private key
95105
X509Certificate clientCertificate = createSelfSignedCertificate(clientKeyPair);
96106

97-
// Create a new local key store containing the private key and the certificate
107+
// Create a new local key store containing the client private key and the certificate
98108
createKeyStore(clientKeyPair.getPrivate(), clientCertificate);
99109

110+
// Add the client certificate that we just generated to the trust store of the server.
111+
// That way the server will trust our certificate.
100112
addCertificateToContainerTrustStore(clientCertificate);
101113

102114
return create(WebArchive.class)
@@ -125,7 +137,7 @@ public void setup() throws FileNotFoundException, IOException {
125137

126138

127139
webClient = new WebClient();
128-
140+
129141
// Server -> client : the trust store certificates are used to validate the certificate sent
130142
// by the server
131143

@@ -141,6 +153,7 @@ public void setup() throws FileNotFoundException, IOException {
141153
// and sent a reply to the server
142154
webClient.getOptions().setSSLClientCertificate(new File(keyStorePath).toURI().toURL(), "changeit", "jks");
143155

156+
144157
}
145158

146159
@After
@@ -198,7 +211,7 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws
198211
SSLSocketFactory factory = context.getSocketFactory();
199212

200213
try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
201-
socket.setSoTimeout(15000);
214+
socket.setSoTimeout(0);
202215
socket.startHandshake();
203216
socket.close();
204217
}
@@ -292,5 +305,18 @@ private static void createTrustStore(X509Certificate[] certificates) {
292305
ex.printStackTrace();
293306
}
294307
}
308+
309+
private static void enableSSLDebug() {
310+
System.setProperty("javax.net.debug", "ssl:handshake");
311+
312+
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "debug");
313+
Logger.getLogger("com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory").setLevel(FINEST);
314+
Logger.getLogger("org.apache.http.conn.ssl.SSLConnectionSocketFactory").setLevel(FINEST);
315+
Log logger = LogFactory.getLog(org.apache.http.conn.ssl.SSLConnectionSocketFactory.class);
316+
((Jdk14Logger) logger).getLogger().setLevel(FINEST);
317+
logger = LogFactory.getLog(com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.class);
318+
((Jdk14Logger) logger).getLogger().setLevel(FINEST);
319+
Logger.getGlobal().getParent().getHandlers()[0].setLevel(FINEST);
320+
}
295321

296322
}

0 commit comments

Comments
 (0)