1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.ssl;
21
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.security.GeneralSecurityException;
26 import java.security.KeyStore;
27
28 import javax.net.ssl.KeyManagerFactory;
29 import javax.net.ssl.SSLContext;
30
31
32
33
34
35
36
37
38 public class BogusSSLContextFactory
39 {
40
41
42
43
44 private static final String PROTOCOL = "TLS";
45
46
47
48
49 private static final String BOGUS_KEYSTORE = "/bogus.cert";
50
51
52
53
54
55
56
57
58
59
60 private static final char[] BOGUS_PW =
61 { 'b', 'o', 'g', 'u', 's', 'p', 'w' };
62
63 private static SSLContext serverInstance = null;
64
65 private static SSLContext clientInstance = null;
66
67
68
69
70
71
72
73
74
75 public static SSLContext getInstance( boolean server ) throws GeneralSecurityException
76 {
77 SSLContext retInstance = null;
78 if ( server )
79 {
80 if ( serverInstance == null )
81 {
82 synchronized ( BogusSSLContextFactory.class )
83 {
84 if ( serverInstance == null )
85 {
86 try
87 {
88 serverInstance = createBougusServerSSLContext();
89 }
90 catch ( Exception ioe )
91 {
92 throw new GeneralSecurityException( "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
117 private static SSLContext createBougusServerSSLContext() throws GeneralSecurityException, IOException
118 {
119
120 KeyStore ks = KeyStore.getInstance( "JKS" );
121 InputStream in = null;
122 try
123 {
124 in = BogusSSLContextFactory.class.getResourceAsStream( BOGUS_KEYSTORE );
125 ks.load( in, BOGUS_PW );
126 }
127 finally
128 {
129 if ( in != null )
130 {
131 try
132 {
133 in.close();
134 }
135 catch ( IOException ignored )
136 {
137 }
138 }
139 }
140
141
142 KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
143 kmf.init( ks, BOGUS_PW );
144
145
146 SSLContext sslContext = SSLContext.getInstance( PROTOCOL );
147 sslContext.init( kmf.getKeyManagers(), BogusTrustManagerFactory.X509_MANAGERS, null );
148
149 return sslContext;
150 }
151
152
153 private static SSLContext createBougusClientSSLContext() throws GeneralSecurityException
154 {
155 SSLContext context = SSLContext.getInstance( PROTOCOL );
156 context.init( null, BogusTrustManagerFactory.X509_MANAGERS, null );
157 return context;
158 }
159
160 }