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