1 22 package fr.dyade.aaa.agent; 23 24 import java.io.*; 25 import java.net.*; 26 import javax.net.ssl.*; 27 import java.security.KeyStore ; 28 import javax.security.cert.X509Certificate; 29 import java.util.Vector ; 30 31 import org.objectweb.util.monolog.api.BasicLevel; 32 import org.objectweb.util.monolog.api.Logger; 33 34 import fr.dyade.aaa.util.*; 35 36 40 public final class HttpsNetwork extends HttpNetwork { 41 48 public final static String PASS = "HttpsNetwork.pass"; 49 56 public final static String KEYFILE = "HttpsNetwork.keyfile"; 57 58 61 SSLSocketFactory socketFactory = null; 62 65 SSLServerSocketFactory serverSocketFactory = null; 66 67 public HttpsNetwork() throws Exception { 68 super(); 69 } 70 71 SSLSocketFactory getSocketFactory() throws IOException { 72 if (socketFactory == null) { 73 try { 74 char[] pass = AgentServer.getProperty(PASS, "changeit").toCharArray(); 75 String keyFile = AgentServer.getProperty(KEYFILE, ".keystore"); 76 77 KeyStore ks = KeyStore.getInstance("JKS"); 78 ks.load(new FileInputStream(keyFile), pass); 79 80 83 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); 84 tmf.init(ks); 85 86 SSLContext ctx = SSLContext.getInstance("TLS"); 87 ctx.init(null, tmf.getTrustManagers(), null); 88 89 socketFactory = ctx.getSocketFactory(); 90 } catch (IOException exc) { 91 throw exc; 92 } catch (Exception exc) { 93 logmon.log(BasicLevel.ERROR, 94 this.getName() + ", cannot initialize SSLSocketFactory", exc); 95 throw new IOException(exc.getMessage()); 96 } 97 } 98 return socketFactory; 99 } 100 101 SSLServerSocketFactory getServerSocketFactory() throws IOException { 102 if (serverSocketFactory == null) { 103 try { 104 char[] pass = AgentServer.getProperty(PASS, "changeit").toCharArray(); 105 String keyFile = AgentServer.getProperty(KEYFILE, ".keystore"); 106 107 KeyStore ks = KeyStore.getInstance("JKS"); 108 ks.load(new FileInputStream(keyFile), pass); 109 110 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 111 kmf.init(ks, pass); 112 113 SSLContext ctx = SSLContext.getInstance("TLS"); 114 ctx.init(kmf.getKeyManagers(), null, null); 115 116 serverSocketFactory = ctx.getServerSocketFactory(); 117 } catch (IOException exc) { 118 throw exc; 119 } catch (Exception exc) { 120 logmon.log(BasicLevel.ERROR, 121 this.getName() + ", cannot initialize SSLServerSocketFactory", exc); 122 throw new IOException(exc.getMessage()); 123 } 124 } 125 return serverSocketFactory; 126 } 127 128 137 ServerSocket createServerSocket(int port) throws IOException { 138 ServerSocket serverSocket = null; 139 serverSocket = getServerSocketFactory().createServerSocket(port, backlog); 140 ((SSLServerSocket) serverSocket).setNeedClientAuth(false); 141 142 return serverSocket; 143 } 144 145 156 Socket createSocket(InetAddress host, int port) throws IOException { 157 if (host == null) 158 throw new UnknownHostException(); 159 160 return getSocketFactory().createSocket(host, port); 161 } 162 163 175 Socket createTunnelSocket(InetAddress host, int port, 176 InetAddress proxy, int proxyport) throws IOException { 177 179 Socket tunnel = new Socket(proxy, proxyport); 182 doTunnelHandshake(tunnel, host, port); 183 184 SSLSocket socket = 186 (SSLSocket) getSocketFactory().createSocket(tunnel,host.getHostName(), port, true); 187 socket.addHandshakeCompletedListener( 189 new HandshakeCompletedListener() { 190 public void handshakeCompleted(HandshakeCompletedEvent event) { 191 } 192 }); 193 194 return socket; 195 } 196 197 private void doTunnelHandshake(Socket tunnel, InetAddress host, int port) 198 throws IOException { 199 OutputStream out = tunnel.getOutputStream(); 200 String msg = "CONNECT " + host.getHostName() + ":" + port + " HTTP/1.0\n" 201 + "User-Agent: " 202 + sun.net.www.protocol.http.HttpURLConnection.userAgent 203 + "\r\n\r\n"; 204 byte b[]; 205 try { 206 b = msg.getBytes("ASCII7"); 209 } catch (UnsupportedEncodingException ignored) { 210 b = msg.getBytes(); 213 } 214 out.write(b); 215 out.flush(); 216 217 byte reply[] = new byte[200]; 220 int replyLen = 0; 221 int newlinesSeen = 0; 222 boolean headerDone = false; 223 224 InputStream in = tunnel.getInputStream(); 225 boolean error = false; 226 227 while (newlinesSeen < 2) { 228 int i = in.read(); 229 if (i < 0) { 230 throw new IOException("Unexpected EOF from proxy"); 231 } 232 if (i == '\n') { 233 headerDone = true; 234 ++newlinesSeen; 235 } else if (i != '\r') { 236 newlinesSeen = 0; 237 if (!headerDone && replyLen < reply.length) { 238 reply[replyLen++] = (byte) i; 239 } 240 } 241 } 242 243 String replyStr; 247 try { 248 replyStr = new String (reply, 0, replyLen, "ASCII7"); 249 } catch (UnsupportedEncodingException ignored) { 250 replyStr = new String (reply, 0, replyLen); 251 } 252 253 254 if (! (replyStr.startsWith("HTTP/1.0 200") || 255 (replyStr.startsWith("HTTP/1.1 200")))) { 256 throw new IOException("Unable to tunnel , proxy returns \"" + replyStr + "\""); 257 } 258 259 } 261 } 262 263 | Popular Tags |