1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package uk.ac.rdg.resc.jstyx.ssl;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.FileInputStream;
24 import java.security.GeneralSecurityException;
25 import java.security.KeyStore;
26
27 import javax.net.ssl.KeyManagerFactory;
28 import javax.net.ssl.SSLContext;
29
30 /***
31 * Factory to create a bougus SSLContext.
32 *
33 * @author Per Widerlund (per@minq.se)
34 * @author Jan Andersson (janne@minq.se)
35 *
36 * @version $Rev: 173 $, $Date: 2005-03-24 07:57:41 +0000 (Thu, 24 Mar 2005) $
37 */
38 public class JonSSLContextFactory
39 {
40
41 /***
42 * Protocol to use.
43 */
44 private static final String PROTOCOL = "TLS";
45
46 /***
47 * Bougus Server certificate keystore file name.
48 */
49 private static final String BOGUS_KEYSTORE = "c://bogus.cert";
50
51
52
53
54
55
56
57 /***
58 * Bougus keystore password.
59 */
60 private static final char[] BOGUS_PW = { 'b', 'o', 'g', 'u', 's', 'p',
61 'w' };
62
63 private static SSLContext serverInstance = null;
64
65 private static SSLContext clientInstance = null;
66
67 /***
68 * Get SSLContext singleton.
69 *
70 * @return SSLContext
71 * @throws java.security.GeneralSecurityException
72 *
73 */
74 public static SSLContext getInstance( boolean server, String keystore )
75 throws GeneralSecurityException
76 {
77 SSLContext retInstance = null;
78 if( server )
79 {
80 if( serverInstance == null )
81 {
82 synchronized( JonSSLContextFactory.class )
83 {
84 if( serverInstance == null )
85 {
86 try
87 {
88 serverInstance = createBougusServerSSLContext(keystore);
89 }
90 catch( Exception ioe )
91 {
92 throw new GeneralSecurityException(
93 "Can't create Server SSLContext:" + ioe );
94 }
95 }
96 }
97 }
98 retInstance = serverInstance;
99 }
100 else
101 {
102 if( clientInstance == null )
103 {
104 synchronized( JonSSLContextFactory.class )
105 {
106 if( clientInstance == null )
107 {
108 clientInstance = createBougusClientSSLContext();
109 }
110 }
111 }
112 retInstance = clientInstance;
113 }
114 return retInstance;
115 }
116
117 private static SSLContext createBougusServerSSLContext(String keystore)
118 throws GeneralSecurityException, IOException
119 {
120
121 KeyStore ks = KeyStore.getInstance( "JKS" );
122 InputStream in = null;
123 try
124 {
125
126
127 in = new FileInputStream(keystore);
128 if (in == null)
129 {
130 System.err.println("Could not read keystore");
131 }
132 ks.load( in, BOGUS_PW );
133 }
134 finally
135 {
136 if( in != null )
137 {
138 try
139 {
140 in.close();
141 }
142 catch( IOException ignored )
143 {
144 }
145 }
146 }
147
148
149 KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
150 kmf.init( ks, BOGUS_PW );
151
152
153 SSLContext sslContext = SSLContext.getInstance( PROTOCOL );
154 sslContext.init( kmf.getKeyManagers(),
155 JonTrustManagerFactory.X509_MANAGERS, null );
156
157 return sslContext;
158 }
159
160 private static SSLContext createBougusClientSSLContext()
161 throws GeneralSecurityException
162 {
163 System.err.println("Creating bogus Client SSL context");
164 SSLContext context = SSLContext.getInstance( PROTOCOL );
165 context.init( null, JonTrustManagerFactory.X509_MANAGERS, null );
166 return context;
167 }
168
169 }