3232import java .util .ArrayList ;
3333import java .util .Date ;
3434import java .util .List ;
35- import java .util .logging .Level ;
3635import java .util .logging .Logger ;
3736
3837import javax .net .ssl .SSLContext ;
@@ -84,6 +83,8 @@ public class SecureServletTest {
8483 @ Deployment (testable = false )
8584 public static WebArchive createDeployment () throws FileNotFoundException , IOException {
8685
86+ System .out .println ("\n *********** DEPLOYMENT START ***************************" );
87+
8788 Provider provider = new BouncyCastleProvider ();
8889 Security .addProvider (provider );
8990
@@ -114,93 +115,78 @@ public static WebArchive createDeployment() throws FileNotFoundException, IOExce
114115 addCertificateToContainerTrustStore (clientCertificate );
115116
116117 return create (WebArchive .class )
117- .addClass (SecureServlet .class )
118+ .addClasses (SecureServlet .class )
118119 .addAsWebInfResource ((new File (WEBAPP_SRC + "/WEB-INF" , "web.xml" )))
119120 .addAsWebInfResource ((new File (WEBAPP_SRC + "/WEB-INF" , "glassfish-web.xml" )));
120121 }
121122
122123 @ Before
123124 public void setup () throws FileNotFoundException , IOException {
125+
126+ System .out .println ("\n *********** SETUP START ***************************" );
127+
128+ webClient = new WebClient ();
124129
125- // ### Ask the server for its certificate and add that to a new local trust store
126-
127- // First get the HTTPS url for which the server is listening
130+ // First get the HTTPS URL for which the server is listening
128131 baseHttps = ServerOperations .toContainerHttps (base );
132+ if (baseHttps == null ) {
133+ throw new IllegalStateException ("No https URL could be created from " + base );
134+ }
129135
130- System .out .println ("***************************************" );
131-
132- if (baseHttps != null ) {
133- System .out .println ("Created " + baseHttps );
134- X509Certificate [] serverCertificateChain = getCertificateChainFromServer (baseHttps .getHost (), baseHttps .getPort ());
136+
137+
138+ // ### Ask the server for its certificate and add that to a new local trust store
139+
140+ // Server -> client : the trust store certificates are used to validate the certificate sent
141+ // by the server
142+ X509Certificate [] serverCertificateChain = getCertificateChainFromServer (baseHttps .getHost (), baseHttps .getPort ());
143+
144+ if (serverCertificateChain != null && serverCertificateChain .length > 0 ) {
145+
146+ System .out .println ("Obtained certificate from server. Storing it in client trust store" );
147+
135148 createTrustStore (serverCertificateChain );
149+
150+ String trustStorePath = System .getProperty ("buildDirectory" , "" ) + "/clientTrustStore.jks" ;
151+ System .out .println ("Reading trust store from: " + trustStorePath );
152+
153+ webClient .getOptions ().setSSLTrustStore (new File (trustStorePath ).toURI ().toURL (), "changeit" , "jks" );
136154
155+ // If the use.cnHost property is we try to extract the host from the server
156+ // certificate and use exactly that host for our requests.
157+ // This is needed if a server is listening to multiple host names, for instance
158+ // localhost and example.com. If the certificate is for example.com, we can't
159+ // localhost for the request, as that will not be accepted.
137160 if (System .getProperty ("use.cnHost" ) != null ) {
138- if (serverCertificateChain != null && serverCertificateChain .length > 0 ) {
139- X509Certificate firstCert = serverCertificateChain [0 ];
140- String name = firstCert .getIssuerX500Principal ().getName ();
141- System .out .println ("Full certificate issuer name " + name );
142- String [] names = name .split ("," );
143- // cn should be first
144- if (names != null && names .length > 0 ) {
145- String cnNameString = names [0 ];
146- String cn = cnNameString .substring (cnNameString .indexOf ('=' ) + 1 ).trim ();
147- System .out .println ("Issuer CN name " + cn );
148-
149- try {
150- URL httpsUrl = new URL (
151- baseHttps .getProtocol (),
152- cn ,
153- baseHttps .getPort (),
154- baseHttps .getFile ()
155- );
156-
157- System .out .println ("Changing to " + httpsUrl + " from " + baseHttps );
158-
159- baseHttps = httpsUrl ;
160-
161- } catch (MalformedURLException e ) {
162- System .out .println ("Failure creating HTTPS URL" );
163- e .printStackTrace ();
164- }
165-
166- }
167-
168- }
161+ System .out .println ("use.cnHost set. Trying to grab CN from certificate and use as host for requests." );
162+ baseHttps = getHostFromCertificate (serverCertificateChain , baseHttps );
169163 }
170-
171164 } else {
172- System .out .println ("No https URL could be created from " + base );
165+ System .out .println ("Could not obtain certificates from server. Continuing without custom truststore" );
173166 }
174-
175-
176- webClient = new WebClient ();
177-
178- // Server -> client : the trust store certificates are used to validate the certificate sent
179- // by the server
180-
181- String trustStorePath = System .getProperty ("buildDirectory" , "" ) + "/clientTrustStore.jks" ;
182- System .out .println ("Reading trust store from: " + trustStorePath );
183-
184- webClient .getOptions ().setSSLTrustStore (new File (trustStorePath ).toURI ().toURL (), "changeit" , "jks" );
185-
167+
186168 String keyStorePath = System .getProperty ("buildDirectory" , "" ) + "/clientKeyStore.jks" ;
187169 System .out .println ("Reading key store from: " + keyStorePath );
188170
189171 // Client -> Server : the key store private keys and certificates are used to sign
190172 // and sent a reply to the server
191173 webClient .getOptions ().setSSLClientCertificate (new File (keyStorePath ).toURI ().toURL (), "changeit" , "jks" );
192-
193174
175+ System .out .println ("*********** SETUP DONE ***************************\n " );
194176 }
195177
196178 @ After
197179 public void tearDown () {
198180 webClient .getCookieManager ().clearCookies ();
199181 webClient .close ();
182+ System .out .println ("\n *********** TEST END ***************************\n " );
200183 }
201184
202185 @ Test
203186 public void testGetWithCorrectCredentials () throws Exception {
187+
188+ System .out .println ("\n *********** TEST START ***************************\n " );
189+
204190 try {
205191 TextPage page = webClient .getPage (baseHttps + "SecureServlet" );
206192
@@ -316,7 +302,7 @@ private static void createKeyStore(PrivateKey privateKey, X509Certificate certif
316302
317303 String path = System .getProperty ("buildDirectory" , "" ) + "/clientKeyStore.jks" ;
318304
319- System .out .println ("Storing key store at: " + path );
305+ System .out .println ("Storing client key store at: " + path );
320306
321307 keyStore .store (new FileOutputStream (path ), "changeit" .toCharArray ());
322308 } catch (Exception ex ) {
@@ -343,6 +329,43 @@ private static void createTrustStore(X509Certificate[] certificates) {
343329 }
344330 }
345331
332+ private static URL getHostFromCertificate (X509Certificate [] serverCertificateChain , URL existingURL ) {
333+ X509Certificate firstCert = serverCertificateChain [0 ];
334+ String name = firstCert .getIssuerX500Principal ().getName ();
335+ System .out .println ("Full certificate issuer name " + name );
336+
337+ String [] names = name .split ("," );
338+
339+ // cn should be first
340+ if (names != null && names .length > 0 ) {
341+ String cnNameString = names [0 ];
342+ String cn = cnNameString .substring (cnNameString .indexOf ('=' ) + 1 ).trim ();
343+ System .out .println ("Issuer CN name: \" " + cn + "\" " );
344+
345+ try {
346+ URL httpsUrl = new URL (
347+ existingURL .getProtocol (),
348+ cn ,
349+ existingURL .getPort (),
350+ existingURL .getFile ()
351+ );
352+
353+ System .out .println ("Changing base URL from " + existingURL + " into " + httpsUrl + "\n " );
354+
355+ return httpsUrl ;
356+
357+ } catch (MalformedURLException e ) {
358+ System .out .println ("Failure creating HTTPS URL" );
359+ e .printStackTrace ();
360+ }
361+
362+ }
363+
364+ System .out .println ("FAILED to get CN. Using existing URL: " + existingURL );
365+
366+ return existingURL ;
367+ }
368+
346369 private static void enableSSLDebug () {
347370 System .setProperty ("javax.net.debug" , "ssl:handshake" );
348371
0 commit comments