1 22 package fr.dyade.aaa.agent; 23 24 import java.io.*; 25 import java.net.*; 26 import javax.net.ssl.*; 27 import javax.security.cert.X509Certificate; 28 import java.security.KeyStore ; 29 import java.security.SecureRandom ; 30 31 34 public final class SSLNetwork extends PoolNetwork { 35 public final static String SSLCONTEXT = "fr.dyade.aaa.agent.SSLNetwork.SSLContext"; 36 public final static String KTYPE = "fr.dyade.aaa.agent.SSLNetwork.KeyStoreType"; 37 38 45 public final static String PASS = "SSLNetwork.pass"; 46 53 public final static String KEYFILE = "SSLNetwork.keyfile"; 54 55 SSLSocketFactory socketFactory = null; 56 SSLServerSocketFactory serverSocketFactory = null; 57 58 public SSLNetwork() throws Exception { 59 super(); 60 name = "SSLNetwork#" + AgentServer.getServerId(); 61 62 char[] pass = AgentServer.getProperty(PASS, "changeit").toCharArray(); 63 String keyFile = AgentServer.getProperty(KEYFILE, ".keystore"); 64 65 KeyStore keystore = KeyStore.getInstance(AgentServer.getProperty(KTYPE, "JKS")); 66 keystore.load(new FileInputStream(keyFile), pass); 67 68 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 69 kmf.init(keystore, pass); 70 71 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); 72 tmf.init(keystore); 73 74 SSLContext ctx = SSLContext.getInstance(AgentServer.getProperty(SSLCONTEXT, "TLS")); 75 ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 76 77 socketFactory = ctx.getSocketFactory(); 78 serverSocketFactory = ctx.getServerSocketFactory(); 79 } 80 81 99 Socket createSocket(InetAddress host, int port) throws IOException { 100 return socketFactory.createSocket(host, port); 101 } 102 103 113 ServerSocket createServerSocket(int port) throws IOException { 114 ServerSocket serverSocket = null; 115 serverSocket = serverSocketFactory.createServerSocket(port); 116 ((SSLServerSocket) serverSocket).setNeedClientAuth(true); 117 118 return serverSocket; 119 } 120 121 130 void setSocketOption(Socket sock) throws SocketException { 131 sock.setTcpNoDelay(true); 133 sock.setSoTimeout(0); 135 sock.setSoLinger(true, 60); 137 } 138 139 } 140 | Popular Tags |