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 The Apache Directory Project (dev@directory.apache.org)
33 * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
34 */
35 public class BogusSSLContextFactory
36 {
37
38 /***
39 * Protocol to use.
40 */
41 private static final String PROTOCOL = "TLS";
42
43 /***
44 * Bougus Server certificate keystore file name.
45 */
46 private static final String BOGUS_KEYSTORE = "bogus.cert";
47
48
49
50
51
52
53
54 /***
55 * Bougus keystore password.
56 */
57 private static final char[] BOGUS_PW = { 'b', 'o', 'g', 'u', 's', 'p',
58 'w' };
59
60 private static SSLContext serverInstance = null;
61
62 private static SSLContext clientInstance = null;
63
64 /***
65 * Get SSLContext singleton.
66 *
67 * @return SSLContext
68 * @throws java.security.GeneralSecurityException
69 *
70 */
71 public static SSLContext getInstance( boolean server )
72 throws GeneralSecurityException
73 {
74 SSLContext retInstance = null;
75 if( server )
76 {
77 if( serverInstance == null )
78 {
79 synchronized( BogusSSLContextFactory.class )
80 {
81 if( serverInstance == null )
82 {
83 try
84 {
85 serverInstance = createBougusServerSSLContext();
86 }
87 catch( Exception ioe )
88 {
89 throw new GeneralSecurityException(
90 "Can't create Server SSLContext:" + ioe );
91 }
92 }
93 }
94 }
95 retInstance = serverInstance;
96 }
97 else
98 {
99 if( clientInstance == null )
100 {
101 synchronized( BogusSSLContextFactory.class )
102 {
103 if( clientInstance == null )
104 {
105 clientInstance = createBougusClientSSLContext();
106 }
107 }
108 }
109 retInstance = clientInstance;
110 }
111 return retInstance;
112 }
113
114 private static SSLContext createBougusServerSSLContext()
115 throws GeneralSecurityException, IOException
116 {
117
118 KeyStore ks = KeyStore.getInstance( "JKS" );
119 InputStream in = null;
120 try
121 {
122 in = BogusSSLContextFactory.class
123 .getResourceAsStream( BOGUS_KEYSTORE );
124 ks.load( in, BOGUS_PW );
125 }
126 finally
127 {
128 if( in != null )
129 {
130 try
131 {
132 in.close();
133 }
134 catch( IOException ignored )
135 {
136 }
137 }
138 }
139
140
141 KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
142 kmf.init( ks, BOGUS_PW );
143
144
145 SSLContext sslContext = SSLContext.getInstance( PROTOCOL );
146 sslContext.init( kmf.getKeyManagers(),
147 BogusTrustManagerFactory.X509_MANAGERS, null );
148
149 return sslContext;
150 }
151
152 private static SSLContext createBougusClientSSLContext()
153 throws GeneralSecurityException
154 {
155 SSLContext context = SSLContext.getInstance( PROTOCOL );
156 context.init( null, BogusTrustManagerFactory.X509_MANAGERS, null );
157 return context;
158 }
159
160 }