1 22 package org.jboss.test.security.service; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.InputStream ; 27 import java.io.IOException ; 28 import java.net.JarURLConnection ; 29 import java.net.Socket ; 30 import java.net.URL ; 31 import java.net.HttpURLConnection ; 32 import java.net.MalformedURLException ; 33 import java.security.Provider ; 34 import java.security.Security ; 35 import java.util.StringTokenizer ; 36 import java.util.jar.JarEntry ; 37 import java.util.jar.JarFile ; 38 import javax.net.ssl.SSLSocketFactory; 39 40 import org.jboss.logging.Logger; 41 import org.jboss.system.ServiceMBeanSupport; 42 import org.jboss.invocation.http.interfaces.Util; 43 import org.jboss.test.util.SecurityProviderUtil; 44 45 51 public class HttpsClient extends ServiceMBeanSupport 52 implements HttpsClientMBean 53 { 54 56 private boolean addedHttpsHandler; 58 59 private boolean addedJSSEProvider; 60 61 63 public HttpsClient() 65 { 66 } 67 68 public String getName() 69 { 70 return "HttpsClient"; 71 } 72 73 74 public String readURL(String urlString) throws IOException 75 { 76 try 77 { 78 String reply = internalReadURL(urlString); 79 log.debug("readURL -> "+reply); 80 return reply; 81 } 82 catch(Throwable e) 83 { 84 log.error("Failed to readURL", e); 85 throw new IOException ("Failed to readURL, ex="+e.getMessage()); 86 } 87 } 88 private String internalReadURL(String urlString) throws Exception 89 { 90 log.debug("Creating URL from string: "+urlString); 91 URL url = new URL (urlString); 92 log.debug("Created URL object from string, protocol="+url.getProtocol()); 93 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 94 97 System.setProperty("org.jboss.security.ignoreHttpsHost", "true"); 98 Util.configureHttpsHostVerifier(conn); 99 100 log.debug("Connecting to URL: "+url); 101 byte[] buffer = new byte[1024]; 102 int length = conn.getContentLength(); 103 log.debug("ContentLength: "+length); 104 InputStream is = conn.getInputStream(); 105 StringBuffer reply = new StringBuffer (); 106 while( (length = is.read(buffer)) > 0 ) 107 reply.append(new String (buffer, 0, length)); 108 log.debug("Done, closing streams"); 109 is.close(); 110 return reply.toString(); 111 } 112 113 protected void startService() throws Exception 115 { 116 addedJSSEProvider = false; 117 try 118 { 119 new URL ("https://www.https.test"); 120 } 121 catch(MalformedURLException e) 122 { 123 Provider provider = SecurityProviderUtil.getJSSEProvider(); 125 log.debug("Adding " + provider.getName()); 126 127 addedJSSEProvider = Security.addProvider(provider) != -1; 128 if (addedJSSEProvider) 129 { 130 log.debug("Added " + provider.getName()); 131 } 132 133 addedHttpsHandler = false; 134 String protocolHandler = SecurityProviderUtil.getProtocolHandlerName(); 136 137 String handlers = System.getProperty("java.protocol.handler.pkgs"); 138 if( handlers == null || handlers.indexOf(protocolHandler ) < 0 ) 139 { 140 handlers += "|" + protocolHandler; 141 log.debug("Adding https handler to java.protocol.handler.pkgs"); 142 System.setProperty("java.protocol.handler.pkgs", handlers); 143 addedHttpsHandler = true; 144 } 145 } 146 147 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 149 URL keyStoreURL = loader.getResource("META-INF/tst.keystore"); 150 if( keyStoreURL == null ) 151 throw new IOException ("Failed to find resource tst.keystore"); 152 if( keyStoreURL.getProtocol().equals("jar") ) 153 { 154 JarURLConnection conn = (JarURLConnection ) keyStoreURL.openConnection(); 155 JarFile jar = conn.getJarFile(); 156 JarEntry entry = jar.getJarEntry("META-INF/tst.keystore"); 157 InputStream is = jar.getInputStream(entry); 158 File tmp = File.createTempFile("tst-", ".keystore"); 159 tmp.deleteOnExit(); 160 FileOutputStream fos = new FileOutputStream (tmp); 161 byte[] buffer = new byte[1024]; 162 int bytes; 163 while( (bytes = is.read(buffer)) > 0 ) 164 fos.write(buffer, 0, bytes); 165 fos.close(); 166 is.close(); 167 keyStoreURL = tmp.toURL(); 168 } 169 log.debug("Setting javax.net.ssl.trustStore to: "+keyStoreURL.getPath()); 170 System.setProperty("javax.net.ssl.trustStore", keyStoreURL.getPath()); 171 } 172 protected void stopService() throws Exception 173 { 174 if (addedJSSEProvider) 175 { 176 Provider provider = SecurityProviderUtil.getJSSEProvider(); 177 String name = provider.getName(); 178 log.debug("Removing " + name); 179 Security.removeProvider(name); 180 } 181 182 if( addedHttpsHandler == true ) 183 { 184 log.debug("Removing https handler from java.protocol.handler.pkgs"); 185 String protocolHandler = SecurityProviderUtil.getProtocolHandlerName(); 186 String handlers = System.getProperty("java.protocol.handler.pkgs"); 187 StringTokenizer tokenizer = new StringTokenizer (handlers, "|"); 188 StringBuffer buffer = new StringBuffer (); 189 while( tokenizer.hasMoreTokens() ) 190 { 191 String handler = tokenizer.nextToken(); 192 if( handler.equals(protocolHandler) == false ) 193 { 194 buffer.append('|'); 195 buffer.append(handler); 196 } 197 } 198 System.setProperty("java.protocol.handler.pkgs", buffer.toString()); 199 } 200 } 201 202 204 class DebugSSLSocketFactory extends SSLSocketFactory 205 { 206 SSLSocketFactory factoryDelegate; 207 Logger theLog; 208 DebugSSLSocketFactory(SSLSocketFactory factoryDelegate, Logger theLog) 209 { 210 this.factoryDelegate = factoryDelegate; 211 this.theLog = theLog; 212 } 213 214 public Socket createSocket(java.net.InetAddress host, int port) throws java.io.IOException 215 { 216 theLog.debug("createSocket, host="+host+", port="+port); 217 Socket s = factoryDelegate.createSocket(host, port); 218 theLog.debug("created socket="+s); 219 return s; 220 } 221 222 public Socket createSocket(String host, int port) 223 throws java.io.IOException , java.net.UnknownHostException 224 { 225 theLog.debug("createSocket, host="+host+", port="+port); 226 Socket s = factoryDelegate.createSocket(host, port); 227 theLog.debug("created socket="+s); 228 return s; 229 } 230 231 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) 232 throws java.io.IOException 233 { 234 theLog.debug("createSocket, socket="+socket+", host="+host+", port="+port); 235 Socket s = factoryDelegate.createSocket(socket, host, port, autoClose); 236 theLog.debug("created socket="+s); 237 return s; 238 } 239 240 public Socket createSocket(java.net.InetAddress host, int port, java.net.InetAddress clientAddress, int clientPort) 241 throws java.io.IOException 242 { 243 theLog.debug("createSocket, host="+host+", port="+port+", clientAddress="+clientAddress+", clientPort="+clientPort); 244 Socket s = factoryDelegate.createSocket(host, port, clientAddress, clientPort); 245 theLog.debug("created socket="+s); 246 return s; 247 } 248 249 public Socket createSocket(String host, int port, java.net.InetAddress clientAddress, int clientPort) 250 throws java.io.IOException , java.net.UnknownHostException 251 { 252 theLog.debug("createSocket, host="+host+", port="+port+", addr="+clientAddress); 253 Socket s = factoryDelegate.createSocket(host, port, clientAddress, clientPort); 254 theLog.debug("created socket="+s); 255 return s; 256 } 257 258 public String [] getDefaultCipherSuites() 259 { 260 return factoryDelegate.getDefaultCipherSuites(); 261 } 262 263 public String [] getSupportedCipherSuites() 264 { 265 return factoryDelegate.getSupportedCipherSuites(); 266 } 267 } 268 269 } 270 | Popular Tags |