KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > net > MantaSSLFactory


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Uri Schneider.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.net;
47
48 import java.io.FileInputStream JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.net.ServerSocket JavaDoc;
51 import java.net.Socket JavaDoc;
52 import java.security.KeyStore JavaDoc;
53
54 import javax.net.ServerSocketFactory;
55 import javax.net.SocketFactory;
56 import javax.net.ssl.KeyManager;
57 import javax.net.ssl.KeyManagerFactory;
58 import javax.net.ssl.SSLContext;
59 import javax.net.ssl.TrustManager;
60 import javax.net.ssl.TrustManagerFactory;
61
62 import org.apache.commons.logging.Log;
63 import org.apache.commons.logging.LogFactory;
64 import org.mr.MantaAgent;
65 import org.mr.core.configuration.ConfigManager;
66 import org.mr.core.log.StartupLogger;
67
68 /**
69  * MantaSSLFactory.java
70  *
71  * A factory class for SSL sockets and server sockets.
72  *
73  * Created: Tue Sep 28 10:57:26 2004
74  *
75  * @author Uri Schneider
76  * @version 1.0
77  */

78 public class MantaSSLFactory {
79     private static MantaSSLFactory instance;
80
81     public static MantaSSLFactory getInstance() {
82         synchronized (MantaSSLFactory.class) {
83             if (instance == null) {
84                 instance = new MantaSSLFactory();
85             }
86         }
87         return instance;
88     }
89
90     private ServerSocketFactory serverSocketFactory;
91     private SocketFactory socketFactory;
92     private Log log;
93
94     private MantaSSLFactory() {
95         this.log=LogFactory.getLog("MantaSSLFactory");
96
97
98         ConfigManager config = MantaAgent.getInstance().getSingletonRepository().getConfigManager();
99
100         String JavaDoc keystoreFile =
101             config.getStringProperty("network.ssl.keystore", "./config/keystore");
102         String JavaDoc algorithm = config.getStringProperty("network.ssl.algorithm", "sunx509");
103         String JavaDoc keystorePasswd =
104             config.getStringProperty("network.ssl.keystore_password", "mantaray");
105
106         try {
107             // Local references used for clarity. Their presence
108
// here is part of the reason we need to import
109
// so many classes.
110
KeyManagerFactory kmf;
111             KeyManager[] km;
112             KeyStore JavaDoc ks;
113             TrustManagerFactory tmf;
114             TrustManager[] tm;
115             SSLContext sslc;
116
117             // Create a keystore that will read the JKS (Java
118
// KeyStore) file format which was created by the keytool
119
// utility.
120
ks = KeyStore.getInstance("JKS");
121
122             // Load the keystore object with the binary keystore file
123
// and a byte array representing its password.
124
ks.load(new FileInputStream JavaDoc(keystoreFile),
125                     keystorePasswd.toCharArray());
126
127             // Gives us a factory for key managers that will let us
128
// handle the asymetric keys we created earlier.
129
kmf = KeyManagerFactory.getInstance(algorithm);
130
131             // Initialize the key manager factory with the keystore
132
// object, again using the same password for security
133
// since it is going to access the private key.
134
kmf.init(ks, keystorePasswd.toCharArray());
135
136             // Now we can get the key managers from the factory, since
137
// it knows what type we are using now.
138
km = kmf.getKeyManagers();
139
140             // Next, create a trust manager factory using the same
141
// algorithm. This is to avoid using the certificates in
142
// cacerts that represent an authentication security risk.
143
tmf = TrustManagerFactory.getInstance(algorithm);
144
145             // ...then initialize it with the keystore object. This
146
// time we don't need the keystore password. This is
147
// because trusted certificates are not a sensitive
148
// element in the keystore, unlike the private keys.
149
tmf.init(ks);
150
151             // Once that's initialized, get the trust managers from
152
// the factory.
153
tm = tmf.getTrustManagers();
154
155             // Almost done, we need a context object that will get our
156
// server socket factory. We specify TLS to indicate that
157
// we will need a server socket factory that supports SSL.
158
sslc = SSLContext.getInstance("TLS");
159
160             // Initialize the context object with the key managers and
161
// trust managers we got earlier. The third parameter is
162
// an optional SecureRandom object. By passing in null, we
163
// are letting the context object create its own.
164
sslc.init(km, tm, null);
165             this.serverSocketFactory = sslc.getServerSocketFactory();
166             this.socketFactory = sslc.getSocketFactory();
167         } catch (Exception JavaDoc e) {
168 // if(log.isErrorEnabled()){
169
// log.error("Exception while initializing MantaSSLFactory: " +
170
// e.getMessage()+".");
171
// }
172
StartupLogger.log.error("MantaSSLFactory ERROR Exception while " +
173                                     "initializing MantaSSLFactory: " +
174                                     e.getMessage(), "MantaSSLFactory");
175         }
176     } // MantaSSLFactory constructor
177

178     public ServerSocket JavaDoc createServerSocket() throws IOException JavaDoc {
179         if (this.serverSocketFactory != null) {
180             return this.serverSocketFactory.createServerSocket();
181         } else {
182             return null;
183         }
184     }
185
186     public Socket JavaDoc createSocket() throws IOException JavaDoc {
187         if (this.socketFactory != null) {
188             return this.socketFactory.createSocket();
189         } else {
190             return null;
191         }
192     }
193
194 } // MantaSSLFactory
195
Popular Tags