-
Notifications
You must be signed in to change notification settings - Fork 78
/
ClientCertTests.java
303 lines (254 loc) · 12 KB
/
ClientCertTests.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
/*
* Copyright (c) 2007, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package servlet.tck.spec.security.clientcert;
import servlet.tck.util.WebUtil;
import servlet.tck.common.client.AbstractUrlClient;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.OperateOnDeployment;
import org.jboss.arquillian.container.test.api.TargetsContainer;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Raja Perumal
*/
public class ClientCertTests extends AbstractUrlClient {
// TOFIX
private static final Logger LOGGER = LoggerFactory.getLogger(ClientCertTests.class);
/**
* Deployment for the test
*/
@Deployment(testable = false, name = "webapp-https")
@TargetsContainer("https")
public static WebArchive getTestArchive() throws Exception {
System.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.TRUE.toString());
// try {
// // specific glassfish so ignore any issue with
// File clientKeyStoreFile = new File("target/test-classes/certificates/clientcert.jks");
// assertTrue(clientKeyStoreFile.exists());
// System.setProperty("javax.net.ssl.keyStore", clientKeyStoreFile.getAbsolutePath());
// System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
//
// File clientPublicCertFile = new File("target/test-classes/certificates/cts_cert");
// assertTrue(clientPublicCertFile.exists());
//
// Collection<? extends Certificate> clientPublicCertificates;
// try {
// clientPublicCertificates = CertificateFactory.getInstance("X.509")
// .generateCertificates(new ByteArrayInputStream(readFileToByteArray(clientPublicCertFile)));
// } catch (CertificateException | IOException e) {
// throw new IllegalStateException(e);
// }
//
// for (Certificate certificate : clientPublicCertificates) {
// addCertificateToContainerTrustStore("tcktest", certificate);
// }
//
// String keystorePath = System.getProperty("keystore.path");
// if (keystorePath!=null) {
// Path trustStorePath = Paths.get(keystorePath);
// assertNotNull(trustStorePath);
// assertTrue(Files.exists(trustStorePath));
//
// LOGGER.info("Using truststore from: {}", trustStorePath.toAbsolutePath());
//
// System.setProperty("javax.net.ssl.trustStore", trustStorePath.toAbsolutePath().toString());
// }
//
// System.setProperty("jdk.tls.client.protocols", "TLSv1.2");
// } catch (Throwable e) {
// LOGGER.debug("ignore glassfish specific", e);
// }
return ShrinkWrap.create(WebArchive.class, "clientcert_web.war")
.addClasses(ServletSecTestServlet.class)
.addAsWebInfResource("servlet/tck/spec/security/clientcert/clientcert_web.war.sun-web.xml",
"sun-web.xml")
.setWebXML(ClientCertTests.class.getResource("clientcert_web.xml"));
}
public static void addCertificateToContainerTrustStore(String alias, Certificate clientCertificate) {
Path cacertsPath = Paths.get(System.getProperty("keystore.path"));
String domain = System.getProperty("domain1");
if (!cacertsPath.toFile().exists()) {
throw new IllegalStateException("The container trust store at " + cacertsPath.toAbsolutePath() + " does not exists");
}
LOGGER.info("*** Adding certificate to container trust store: " + cacertsPath.toAbsolutePath());
KeyStore keyStore = null;
try (InputStream in = new FileInputStream(cacertsPath.toAbsolutePath().toFile())) {
keyStore = KeyStore.getInstance("JKS");
keyStore.load(in, "changeit".toCharArray());
Certificate existingCertificate = keyStore.getCertificate(alias);
if (!clientCertificate.equals(existingCertificate)) {
keyStore.setCertificateEntry(alias, clientCertificate);
keyStore.store(new FileOutputStream(cacertsPath.toAbsolutePath().toFile()), "changeit".toCharArray());
//restartDomain();
} else {
System.out.println("Server's truststore already contains certificate");
}
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
throw new IllegalStateException(e);
}
}
public static Path getAdminCliJar() {
return Paths.get(System.getProperty("glassfish.home") , "glassfish/modules/admin-cli.jar");
}
private static void restartDomain() throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar",
getAdminCliJar().toAbsolutePath().toString(), "restart-domain", "domain1").inheritIO();
processBuilder.start();
}
@ArquillianResource
@OperateOnDeployment("webapp-https") URL urlHttps;
// Configurable constants:
private String hostname = null;
private int portnum = 0;
private String tlsVersion;
private String pageBase = "/clientcert_web";
private String authorizedPage = "/ServletSecTest";
private String user = null;
// Constants:
private final String webHostProp = "webServerHost";
private final String webPortProp = "webServerPort";
private final String failString = "FAILED!";
// DN name for CTS certificate
private final String username = "CN=CTS, OU=Java Software, O=Sun Microsystems Inc., L=Burlington, ST=MA, C=US";
// Shared test variables:
private String request = null;
private WebUtil.Response response = null;
private HostnameVerifier hostnameVerifier;
/*
* @class.setup_props: webServerHost; webServerPort; securedWebServicePort;
* certLoginUserAlias;
*
*/
public void setup(String[] args, Properties p) throws Exception {
hostname = urlHttps.getHost();
if ("localhost".equals(hostname)) {
hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
//for localhost testing only
HttpsURLConnection.setDefaultHostnameVerifier((hostname, sslSession) -> hostname.equals("localhost"));
}
p.setProperty(webHostProp, hostname);
portnum = Integer.getInteger("securedWebServicePort", urlHttps.getPort());// Integer.parseInt(p.getProperty("securedWebServicePort"));
p.setProperty("securedWebServicePort", Integer.toString(portnum));
tlsVersion = System.getProperty("client.cert.test.jdk.tls.client.protocols", p.getProperty("client.cert.test.jdk.tls.client.protocols"));
logger.info("securedWebServicePort = {}", p.getProperty("securedWebServicePort"));
if (tlsVersion != null) {
logger.info("client.cert.test.jdk.tls.client.protocols = {}", tlsVersion);
}
}
@AfterEach
public void cleanup() {
if (hostnameVerifier != null) {
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
}
}
/*
* @testName: clientCertTest
*
* @assertion_ids: Servlet:SPEC:140; Servlet:SPEC:368; Servlet:SPEC:369;
* Servlet:SPEC:26; Servlet:SPEC:26.1; Servlet:SPEC:26.2; Servlet:SPEC:26.3;
* Servlet:JAVADOC:356
*
* @test_strategy: 1. Look for the following request attributes a)
* cipher-suite b) key-size c) SSL certificate If any of the above attributes
* are not set report test failure.
*
* 2. Verify the request.getAuthType returns CLIENT_CERT
*
* Note: If a request has been transmitted over a secure protocol, such as
* HTTPS, this information must be exposed via the isSecure method of the
* ServletRequest interface. The web container must expose the following
* attributes to the servlet programmer. 1) The cipher suite 2) the bit size
* of the algorithm
*
* If there is an SSL certificate associated with the request, it must be
* exposed by the servlet container to the servlet programmer as an array of
* objects of type java.security.cert.X509Certificate
*
*/
@Test
public void clientCertTest() throws Exception {
String testName = "clientCertTest";
String url = getURLString("https", hostname, portnum, pageBase.substring(1) + authorizedPage);
if (tlsVersion != null) {
System.setProperty("jdk.tls.client.protocols", tlsVersion);
}
URL newURL = new URL(url);
// open HttpsURLConnection using TSHttpsURLConnection
URLConnection httpsURLConn = getHttpsURLConnection(newURL);
try (InputStream content = httpsURLConn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content))) {
StringBuilder output = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
output.append(line);
logger.trace("line: {}", line);
}
// compare getRemoteUser() obtained from server's response
// with the username stored in ts.jte
//
// Even though the output need not be identical (because
// of appserver realms) the output should have substring
// match for username stored in ts.jte.
//
String userNameToSearch = username;
if (output.indexOf(userNameToSearch) == -1) {
throw new Exception(testName + ": getRemoteUser(): " + "- did not find \""
+ userNameToSearch + "\" in log.");
} else {
logger.debug("Additional verification done");
}
// verify output for expected test result
verifyTestOutput(output.toString(), testName);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new Exception(testName + ": FAILED", e);
}
}
public void verifyTestOutput(String output, String testName) throws Exception {
// check for the occurance of <testName>+": PASSED"
// message in server's response. If this message is not present
// report test failure.
if (!output.contains(testName + ": PASSED")) {
logger.error("Test {} Not Expected String from the output = {}: PASSED", testName, output);
throw new Exception(testName + ": FAILED");
}
}
}