|
13 | 13 | import java.io.FileNotFoundException; |
14 | 14 | import java.io.FileOutputStream; |
15 | 15 | import java.io.IOException; |
| 16 | +import java.net.MalformedURLException; |
16 | 17 | import java.net.URL; |
17 | 18 | import java.security.KeyManagementException; |
18 | 19 | import java.security.KeyPair; |
|
31 | 32 | import java.util.ArrayList; |
32 | 33 | import java.util.Date; |
33 | 34 | import java.util.List; |
| 35 | +import java.util.logging.Level; |
34 | 36 | import java.util.logging.Logger; |
35 | 37 |
|
36 | 38 | import javax.net.ssl.SSLContext; |
@@ -131,6 +133,41 @@ public void setup() throws FileNotFoundException, IOException { |
131 | 133 | System.out.println("Created " + baseHttps); |
132 | 134 | X509Certificate[] serverCertificateChain = getCertificateChainFromServer(baseHttps.getHost(), baseHttps.getPort()); |
133 | 135 | 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 | + |
134 | 171 | } else { |
135 | 172 | System.out.println("No https URL could be created from " + base); |
136 | 173 | } |
|
0 commit comments