View Javadoc

1   /*
2    *   @(#) $Id: BogusSSLContextFactory.java 332218 2005-11-10 03:52:42Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
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      // NOTE: The keystore was generated using keytool:
49      //   keytool -genkey -alias bogus -keysize 512 -validity 3650
50      //           -keyalg RSA -dname "CN=bogus.com, OU=XXX CA,
51      //               O=Bogus Inc, L=Stockholm, S=Stockholm, C=SE"
52      //           -keypass boguspw -storepass boguspw -keystore bogus.cert
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         // Create keystore
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         // Set up key manager factory to use our key store
141         KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
142         kmf.init( ks, BOGUS_PW );
143 
144         // Initialize the SSLContext to work with our key managers.
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 }