/Users/lyon/j4p/src/net/rmi/ssl/RmiClientServerFactory.java

1    package net.rmi.ssl; 
2     
3    /** 
4     * Copyright DocJava, inc. User: lyon 
5     * <p/> 
6     * Date: Dec 6, 2004 
7     * <p/> 
8     * Time: 8:55:32 AM 
9     */ 
10   //RmiClientServerFactory 
11   import gui.JInfoFrame; 
12    
13   import javax.net.ssl.*; 
14   import java.io.*; 
15   import java.rmi.server.RMIClientSocketFactory; 
16   import java.net.Socket; 
17   import java.security.KeyStore; 
18    
19   /** 
20    * Created by IntelliJ IDEA. User: pkrepszt Date: Nov 16, 2004 Time: 
21    * 7:24:57 PM To change this template use File | Settings | File 
22    * Templates. 
23    */ 
24   public class RmiClientServerFactory implements RMIClientSocketFactory, 
25                                                     Serializable { 
26       private static JInfoFrame jif2 = new JInfoFrame(); 
27       public static int port2 = 1024; 
28       public static void main(String args[]){ 
29           RmiClientServerFactory rcsf = new RmiClientServerFactory(); 
30           try { 
31               Socket s = rcsf.createSocket("wow",12); 
32           } catch (IOException e) { 
33               e.printStackTrace(); 
34    
35           } 
36       } 
37    
38       public Socket createSocket(String IpAddress, int port) 
39               throws IOException { 
40           jif2.setSize(400, 400); 
41           jif2.show(); 
42           // System.out.println("Client ip "+IpAddress); 
43           jif2.println("Client ip " + IpAddress); 
44           // System.out.println("Client port "+port); 
45           jif2.println("Client port " + port2); 
46           SSLSocketFactory ssf = null; 
47           try { 
48               char[] passphrase = "ewunia27".toCharArray(); 
49               SSLContext ctx = SSLContext.getInstance("TLS"); 
50               KeyStore ks = KeyStore.getInstance("JKS"); 
51               ks.load(new FileInputStream("clientKeyStore"), passphrase); 
52               KeyStore ts = KeyStore.getInstance("JKS"); 
53               ts.load(new FileInputStream("clientTrustStore"), null); 
54               KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 
55               kmf.init(ks, passphrase); 
56               TrustManagerFactory tmf = 
57                       TrustManagerFactory.getInstance("SunX509"); 
58               tmf.init(ts); 
59               ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 
60               //System.out.println("client KS "+ ks.getProvider().toString());b 
61               // System.out.println("client TMF "+ 
62               tmf.getTrustManagers().toString(); 
63               ssf = ctx.getSocketFactory(); 
64               // System.out.println("client Ciphers "+ 
65               ssf.getSupportedCipherSuites().toString(); 
66           } catch (Exception e) { 
67               jif2.println(getStackTrace(e)); 
68           } 
69           jif2.println("client2"); 
70    
71           return (SSLSocket) ssf.createSocket(IpAddress, port2); 
72       } 
73    
74       public static String getStackTrace(Exception e) { 
75           ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
76           PrintStream ps = new PrintStream(baos); 
77           e.printStackTrace(ps); 
78           try { 
79               baos.close(); 
80           } catch (IOException e1) { 
81               e1.printStackTrace(); 
82    
83           } 
84           return new String(baos.toByteArray()); 
85       } 
86   }