View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.echoserver.ssl;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.security.GeneralSecurityException;
25  import java.security.KeyStore;
26  import java.security.Security;
27  
28  import javax.net.ssl.KeyManagerFactory;
29  import javax.net.ssl.SSLContext;
30  
31  /**
32   * Factory to create a bogus SSLContext.
33   *
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev: 668593 $, $Date: 2008-06-17 11:46:50 +0200 (mar, 17 jun 2008) $
36   */
37  public class BogusSslContextFactory {
38  
39      /**
40       * Protocol to use.
41       */
42      private static final String PROTOCOL = "TLS";
43  
44      private static final String KEY_MANAGER_FACTORY_ALGORITHM;
45  
46      static {
47          String algorithm = Security
48                  .getProperty("ssl.KeyManagerFactory.algorithm");
49          if (algorithm == null) {
50              algorithm = "SunX509";
51          }
52  
53          KEY_MANAGER_FACTORY_ALGORITHM = algorithm;
54      }
55  
56      /**
57       * Bougus Server certificate keystore file name.
58       */
59      private static final String BOGUS_KEYSTORE = "bogus.cert";
60  
61      // NOTE: The keystore was generated using keytool:
62      //   keytool -genkey -alias bogus -keysize 512 -validity 3650
63      //           -keyalg RSA -dname "CN=bogus.com, OU=XXX CA,
64      //               O=Bogus Inc, L=Stockholm, S=Stockholm, C=SE"
65      //           -keypass boguspw -storepass boguspw -keystore bogus.cert
66  
67      /**
68       * Bougus keystore password.
69       */
70      private static final char[] BOGUS_PW = { 'b', 'o', 'g', 'u', 's', 'p', 'w' };
71  
72      private static SSLContext serverInstance = null;
73      
74      private static SSLContext clientInstance = null;
75  
76      /**
77       * Get SSLContext singleton.
78       *
79       * @return SSLContext
80       * @throws java.security.GeneralSecurityException
81       *
82       */
83      public static SSLContext getInstance(boolean server)
84              throws GeneralSecurityException {
85          SSLContext retInstance = null;
86          if (server) {
87              synchronized(BogusSslContextFactory.class) {
88                  if (serverInstance == null) {
89                      try {
90                          serverInstance = createBougusServerSslContext();
91                      } catch (Exception ioe) {
92                          throw new GeneralSecurityException(
93                                  "Can't create Server SSLContext:" + ioe);
94                      }
95                  }
96              }
97              retInstance = serverInstance;
98          } else {
99              synchronized (BogusSslContextFactory.class) {
100                 if (clientInstance == null) {
101                     clientInstance = createBougusClientSslContext();
102                 }
103             }
104             retInstance = clientInstance;
105         }
106         return retInstance;
107     }
108 
109     private static SSLContext createBougusServerSslContext()
110             throws GeneralSecurityException, IOException {
111         // Create keystore
112         KeyStore ks = KeyStore.getInstance("JKS");
113         InputStream in = null;
114         try {
115             in = BogusSslContextFactory.class
116                     .getResourceAsStream(BOGUS_KEYSTORE);
117             ks.load(in, BOGUS_PW);
118         } finally {
119             if (in != null) {
120                 try {
121                     in.close();
122                 } catch (IOException ignored) {
123                 }
124             }
125         }
126 
127         // Set up key manager factory to use our key store
128         KeyManagerFactory kmf = KeyManagerFactory
129                 .getInstance(KEY_MANAGER_FACTORY_ALGORITHM);
130         kmf.init(ks, BOGUS_PW);
131 
132         // Initialize the SSLContext to work with our key managers.
133         SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
134         sslContext.init(kmf.getKeyManagers(),
135                 BogusTrustManagerFactory.X509_MANAGERS, null);
136 
137         return sslContext;
138     }
139 
140     private static SSLContext createBougusClientSslContext()
141             throws GeneralSecurityException {
142         SSLContext context = SSLContext.getInstance(PROTOCOL);
143         context.init(null, BogusTrustManagerFactory.X509_MANAGERS, null);
144         return context;
145     }
146 
147 }