View Javadoc

1   /*
2    *   @(#) $Id: JonSSLContextFactory.java 173 2005-03-24 07:57:41Z jonblower $
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 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      // NOTE: The keystore was generated using keytool:
52      //   keytool -genkey -alias bogus -keysize 512 -validity 3650
53      //           -keyalg RSA -dname "CN=bogus.com, OU=XXX CA,
54      //               O=Bogus Inc, L=Stockholm, S=Stockholm, C=SE"
55      //           -keypass boguspw -storepass boguspw -keystore bogus.cert
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         // Create keystore
121         KeyStore ks = KeyStore.getInstance( "JKS" );
122         InputStream in = null;
123         try
124         {
125             //in = JonSSLContextFactory.class
126             //        .getResourceAsStream( BOGUS_KEYSTORE );
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         // Set up key manager factory to use our key store
149         KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
150         kmf.init( ks, BOGUS_PW );
151 
152         // Initialize the SSLContext to work with our key managers.
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 }