View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.example.http.websocketx.sslserver;
17  
18  import org.jboss.netty.logging.InternalLogger;
19  import org.jboss.netty.logging.InternalLoggerFactory;
20  
21  import javax.net.ssl.KeyManagerFactory;
22  import javax.net.ssl.SSLContext;
23  import java.io.FileInputStream;
24  import java.security.KeyStore;
25  import java.security.Security;
26  
27  /**
28   * Creates a {@link SSLContext} for just server certificates.
29   */
30  public final class WebSocketSslServerSslContext {
31  
32      private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketSslServerSslContext.class);
33      private static final String PROTOCOL = "TLS";
34  
35      private final SSLContext _serverContext;
36  
37      /**
38       * Returns the singleton instance for this class
39       */
40      public static WebSocketSslServerSslContext getInstance() {
41          return SingletonHolder.INSTANCE;
42      }
43  
44      /**
45       * SingletonHolder is loaded on the first execution of Singleton.getInstance() or the first access to
46       * SingletonHolder.INSTANCE, not before.
47       *
48       * See http://en.wikipedia.org/wiki/Singleton_pattern
49       */
50      private interface SingletonHolder {
51          WebSocketSslServerSslContext INSTANCE = new WebSocketSslServerSslContext();
52      }
53  
54      /**
55       * Constructor for singleton
56       */
57      private WebSocketSslServerSslContext() {
58          SSLContext serverContext = null;
59          try {
60              // Key store (Server side certificate)
61              String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
62              if (algorithm == null) {
63                  algorithm = "SunX509";
64              }
65  
66              try {
67                  String keyStoreFilePath = System.getProperty("keystore.file.path");
68                  String keyStoreFilePassword = System.getProperty("keystore.file.password");
69  
70                  KeyStore ks = KeyStore.getInstance("JKS");
71                  FileInputStream fin = new FileInputStream(keyStoreFilePath);
72                  ks.load(fin, keyStoreFilePassword.toCharArray());
73  
74                  // Set up key manager factory to use our key store
75                  // Assume key password is the same as the key store file
76                  // password
77                  KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
78                  kmf.init(ks, keyStoreFilePassword.toCharArray());
79  
80                  // Initialise the SSLContext to work with our key managers.
81                  serverContext = SSLContext.getInstance(PROTOCOL);
82                  serverContext.init(kmf.getKeyManagers(), null, null);
83              } catch (Exception e) {
84                  throw new Error("Failed to initialize the server-side SSLContext", e);
85              }
86          } catch (Exception ex) {
87              if (logger.isErrorEnabled()) {
88                  logger.error("Error initializing SslContextManager. " + ex.getMessage(), ex);
89              }
90              System.exit(1);
91          } finally {
92              _serverContext = serverContext;
93          }
94      }
95  
96      /**
97       * Returns the server context with server side key store
98       */
99      public SSLContext getServerContext() {
100         return _serverContext;
101     }
102 }