Skip to content

Commit c724aa4

Browse files
committed
Added option for client-cert to use host from obtained cert
1 parent b8cbb0c commit c724aa4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.FileNotFoundException;
1414
import java.io.FileOutputStream;
1515
import java.io.IOException;
16+
import java.net.MalformedURLException;
1617
import java.net.URL;
1718
import java.security.KeyManagementException;
1819
import java.security.KeyPair;
@@ -31,6 +32,7 @@
3132
import java.util.ArrayList;
3233
import java.util.Date;
3334
import java.util.List;
35+
import java.util.logging.Level;
3436
import java.util.logging.Logger;
3537

3638
import javax.net.ssl.SSLContext;
@@ -131,6 +133,41 @@ public void setup() throws FileNotFoundException, IOException {
131133
System.out.println("Created " + baseHttps);
132134
X509Certificate[] serverCertificateChain = getCertificateChainFromServer(baseHttps.getHost(), baseHttps.getPort());
133135
createTrustStore(serverCertificateChain);
136+
137+
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+
}
169+
}
170+
134171
} else {
135172
System.out.println("No https URL could be created from " + base);
136173
}

0 commit comments

Comments
 (0)