|
47 | 47 | import org.junit.Before; |
48 | 48 | import org.junit.ClassRule; |
49 | 49 | import org.junit.Test; |
| 50 | +import org.junit.experimental.theories.DataPoint; |
| 51 | +import org.junit.experimental.theories.Theories; |
| 52 | +import org.junit.experimental.theories.Theory; |
| 53 | +import org.junit.runner.RunWith; |
50 | 54 |
|
51 | 55 | import static org.assertj.core.api.Assertions.assertThat; |
52 | 56 |
|
| 57 | +@RunWith(Theories.class) |
53 | 58 | public class SSLTest { |
54 | 59 |
|
55 | | - private static final String CLIENT_KEYSTORE = "/SSLTest/clientkeystore.jks"; |
56 | | - private static final String CLIENT_KEYSTORE_PWD = "clientp12pwd"; |
| 60 | + private static final String JKS_PASSWORD = "abcdef"; |
57 | 61 |
|
58 | | - private static final String CLIENT_TRUSTSTORE = "/SSLTest/clienttruststore.jks"; |
59 | | - private static final String CLIENT_TRUSTSTORE_PWD = "clienttruststorepwd"; |
| 62 | + // This truststore contains only the CA used to sign the server certificate |
| 63 | + @DataPoint |
| 64 | + public static final String CLIENT_TRUSTSTORE_WITH_CA = "/SSLTest/client-with-ca.jks"; |
60 | 65 |
|
61 | | - private static final String SERVER_TRUSTSTORE = "/SSLTest/servertruststore.jks"; |
62 | | - private static final String SERVER_TRUSTSTORE_PWD = "servertruststorepwd"; |
| 66 | + // This truststore contains only the server certificate |
| 67 | + @DataPoint |
| 68 | + public static final String CLIENT_TRUSTSTORE_WITH_CERTIFICATE = "/SSLTest/client-with-certificate.jks"; |
63 | 69 |
|
64 | | - private static final String SERVER_KEYSTORE = "/SSLTest/serverkeystore.jks"; |
65 | | - private static final String SERVER_KEYSTORE_PWD = "serverkeystorepwd"; |
| 70 | + private static final String SERVER_TRUSTSTORE = "/SSLTest/server-with-client-ca.jks"; |
| 71 | + private static final String SERVER_KEYSTORE = "/SSLTest/server.jks"; |
| 72 | + private static final String CLIENT_KEYSTORE = "/SSLTest/client.jks"; |
66 | 73 |
|
67 | 74 | private static Server server; |
68 | 75 | private static int httpsPort; |
@@ -109,18 +116,19 @@ private static void startSSLTransparentReverseProxy(boolean requireClientAuth) t |
109 | 116 | server.addConnector(http); |
110 | 117 |
|
111 | 118 | Path serverKeyStore = Paths.get(SSLTest.class.getResource(SERVER_KEYSTORE).toURI()).toAbsolutePath(); |
112 | | - String serverKeyPassword = "serverp12pwd"; |
113 | | - Path serverTrustStore = Paths.get(SSLTest.class.getResource(SERVER_TRUSTSTORE).toURI()).toAbsolutePath(); |
114 | 119 | assertThat(serverKeyStore).exists(); |
115 | | - assertThat(serverTrustStore).exists(); |
116 | 120 |
|
117 | 121 | // SSL Context Factory |
118 | 122 | SslContextFactory sslContextFactory = new SslContextFactory(); |
119 | 123 | sslContextFactory.setKeyStorePath(serverKeyStore.toString()); |
120 | | - sslContextFactory.setKeyStorePassword(SERVER_KEYSTORE_PWD); |
121 | | - sslContextFactory.setKeyManagerPassword(serverKeyPassword); |
122 | | - sslContextFactory.setTrustStorePath(serverTrustStore.toString()); |
123 | | - sslContextFactory.setTrustStorePassword(SERVER_TRUSTSTORE_PWD); |
| 124 | + sslContextFactory.setKeyStorePassword(JKS_PASSWORD); |
| 125 | + sslContextFactory.setKeyManagerPassword(""); |
| 126 | + if ( requireClientAuth) { |
| 127 | + Path serverTrustStore = Paths.get(SSLTest.class.getResource(SERVER_TRUSTSTORE).toURI()).toAbsolutePath(); |
| 128 | + sslContextFactory.setTrustStorePath(serverTrustStore.toString()); |
| 129 | + assertThat(serverTrustStore).exists(); |
| 130 | + sslContextFactory.setTrustStorePassword(JKS_PASSWORD); |
| 131 | + } |
124 | 132 | sslContextFactory.setNeedClientAuth(requireClientAuth); |
125 | 133 | sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", |
126 | 134 | "SSL_DHE_RSA_WITH_DES_CBC_SHA", |
@@ -165,41 +173,67 @@ public void simple_analysis_with_server_and_client_certificate() throws Exceptio |
165 | 173 | assertThat(buildResult.getLastStatus()).isNotEqualTo(0); |
166 | 174 | assertThat(buildResult.getLogs()).contains("javax.net.ssl.SSLHandshakeException"); |
167 | 175 |
|
168 | | - Path clientTruststore = Paths.get(SSLTest.class.getResource(CLIENT_TRUSTSTORE).toURI()).toAbsolutePath(); |
| 176 | + Path clientTruststore = Paths.get(SSLTest.class.getResource(CLIENT_TRUSTSTORE_WITH_CA).toURI()).toAbsolutePath(); |
169 | 177 | assertThat(clientTruststore).exists(); |
170 | 178 | Path clientKeystore = Paths.get(SSLTest.class.getResource(CLIENT_KEYSTORE).toURI()).toAbsolutePath(); |
171 | 179 | assertThat(clientKeystore).exists(); |
172 | 180 |
|
173 | 181 | Map<String, String> params = new HashMap<>(); |
| 182 | + // In the truststore we have the CA allowing to connect to local TLS server |
174 | 183 | params.put("javax.net.ssl.trustStore", clientTruststore.toString()); |
175 | | - params.put("javax.net.ssl.trustStorePassword", CLIENT_TRUSTSTORE_PWD); |
| 184 | + params.put("javax.net.ssl.trustStorePassword", JKS_PASSWORD); |
| 185 | + // The KeyStore is storing the certificate to identify the user |
176 | 186 | params.put("javax.net.ssl.keyStore", clientKeystore.toString()); |
177 | | - params.put("javax.net.ssl.keyStorePassword", CLIENT_KEYSTORE_PWD); |
| 187 | + params.put("javax.net.ssl.keyStorePassword", JKS_PASSWORD); |
178 | 188 |
|
179 | 189 | buildResult = scanner.executeSimpleProject(project("js-sample"), "https://localhost:" + httpsPort, params); |
180 | 190 | assertThat(buildResult.getLastStatus()).isEqualTo(0); |
181 | 191 | } |
182 | 192 |
|
| 193 | + @Test |
| 194 | + public void simple_analysis_with_server_and_without_client_certificate_is_failing() throws Exception { |
| 195 | + startSSLTransparentReverseProxy(true); |
| 196 | + SimpleScanner scanner = new SimpleScanner(); |
| 197 | + BuildResult buildResult = scanner.executeSimpleProject(project("js-sample"), "https://localhost:" + httpsPort); |
| 198 | + |
| 199 | + assertThat(buildResult.getLastStatus()).isNotEqualTo(0); |
| 200 | + assertThat(buildResult.getLogs()).contains("javax.net.ssl.SSLHandshakeException"); |
| 201 | + |
| 202 | + Path clientTruststore = Paths.get(SSLTest.class.getResource(CLIENT_TRUSTSTORE_WITH_CA).toURI()).toAbsolutePath(); |
| 203 | + assertThat(clientTruststore).exists(); |
| 204 | + Path clientKeystore = Paths.get(SSLTest.class.getResource(CLIENT_KEYSTORE).toURI()).toAbsolutePath(); |
| 205 | + assertThat(clientKeystore).exists(); |
| 206 | + |
| 207 | + Map<String, String> params = new HashMap<>(); |
| 208 | + // In the truststore we have the CA allowing to connect to local TLS server |
| 209 | + params.put("javax.net.ssl.trustStore", clientTruststore.toString()); |
| 210 | + params.put("javax.net.ssl.trustStorePassword", JKS_PASSWORD); |
| 211 | + // Voluntary missing client keystore |
| 212 | + |
| 213 | + buildResult = scanner.executeSimpleProject(project("js-sample"), "https://localhost:" + httpsPort, params); |
| 214 | + assertThat(buildResult.getLastStatus()).isEqualTo(1); |
| 215 | + assertThat(buildResult.getLogs()).contains("bad_certificate"); |
| 216 | + } |
| 217 | + |
183 | 218 | private static Path project(String projectName) { |
184 | 219 | return Paths.get("..", "projects", projectName); |
185 | 220 | } |
186 | 221 |
|
187 | | - @Test |
188 | | - public void simple_analysis_with_server_certificate() throws Exception { |
| 222 | + @Theory |
| 223 | + public void simple_analysis_with_server_certificate(String clientTrustStore) throws Exception { |
189 | 224 | startSSLTransparentReverseProxy(false); |
190 | 225 | SimpleScanner scanner = new SimpleScanner(); |
191 | 226 |
|
192 | 227 | BuildResult buildResult = scanner.executeSimpleProject(project("js-sample"), "https://localhost:" + httpsPort); |
193 | 228 | assertThat(buildResult.getLastStatus()).isNotEqualTo(0); |
194 | 229 | assertThat(buildResult.getLogs()).contains("javax.net.ssl.SSLHandshakeException"); |
195 | 230 |
|
196 | | - Path clientTruststore = Paths.get(SSLTest.class.getResource(CLIENT_TRUSTSTORE).toURI()).toAbsolutePath(); |
197 | | - String truststorePassword = CLIENT_TRUSTSTORE_PWD; |
| 231 | + Path clientTruststore = Paths.get(SSLTest.class.getResource(clientTrustStore).toURI()).toAbsolutePath(); |
198 | 232 | assertThat(clientTruststore).exists(); |
199 | 233 |
|
200 | 234 | Map<String, String> params = new HashMap<>(); |
201 | 235 | params.put("javax.net.ssl.trustStore", clientTruststore.toString()); |
202 | | - params.put("javax.net.ssl.trustStorePassword", truststorePassword); |
| 236 | + params.put("javax.net.ssl.trustStorePassword", JKS_PASSWORD); |
203 | 237 |
|
204 | 238 | buildResult = scanner.executeSimpleProject(project("js-sample"), "https://localhost:" + httpsPort, params); |
205 | 239 | assertThat(buildResult.getLastStatus()).isEqualTo(0); |
|
0 commit comments