KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > net > SSLConfig


1 /**
2  * $RCSfile: SSLConfig.java,v $
3  * $Revision: 1.9 $
4  * $Date: 2005/04/11 21:04:00 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.net;
13
14 import org.jivesoftware.util.JiveGlobals;
15 import org.jivesoftware.util.Log;
16
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23 import java.security.KeyStore JavaDoc;
24
25 /**
26  * Configuration of Jive Messenger's SSL settings.
27  *
28  * @author Iain Shigeoka
29  */

30 public class SSLConfig {
31
32     private static SSLJiveServerSocketFactory sslFactory;
33     private static KeyStore JavaDoc keyStore;
34     private static String JavaDoc keypass;
35     private static KeyStore JavaDoc trustStore;
36     private static String JavaDoc trustpass;
37     private static String JavaDoc keyStoreLocation;
38     private static String JavaDoc trustStoreLocation;
39
40     private SSLConfig() {
41     }
42
43     static {
44         String JavaDoc algorithm = JiveGlobals.getProperty("xmpp.socket.ssl.algorithm", "TLS");
45         String JavaDoc storeType = JiveGlobals.getProperty("xmpp.socket.ssl.storeType", "jks");
46
47         // Get the keystore location. The default location is security/keystore
48
keyStoreLocation = JiveGlobals.getProperty("xmpp.socket.ssl.keystore",
49                 "resources" + File.separator + "security" + File.separator + "keystore");
50         keyStoreLocation = JiveGlobals.getHomeDirectory() + File.separator + keyStoreLocation;
51
52         // Get the keystore password. The default password is "changeit".
53
keypass = JiveGlobals.getProperty("xmpp.socket.ssl.keypass", "changeit");
54         keypass = keypass.trim();
55
56         // Get the truststore location; default at security/truststore
57
trustStoreLocation = JiveGlobals.getProperty("xmpp.socket.ssl.truststore",
58                 "resources" + File.separator + "security" + File.separator + "truststore");
59         trustStoreLocation = JiveGlobals.getHomeDirectory() + File.separator + trustStoreLocation;
60
61         // Get the truststore passwprd; default is "changeit".
62
trustpass = JiveGlobals.getProperty("xmpp.socket.ssl.trustpass", "changeit");
63         trustpass = trustpass.trim();
64
65         try {
66             keyStore = KeyStore.getInstance(storeType);
67             keyStore.load(new FileInputStream JavaDoc(keyStoreLocation), keypass.toCharArray());
68
69             trustStore = KeyStore.getInstance(storeType);
70             trustStore.load(new FileInputStream JavaDoc(trustStoreLocation), trustpass.toCharArray());
71
72             sslFactory = (SSLJiveServerSocketFactory)SSLJiveServerSocketFactory.getInstance(
73                     algorithm, keyStore, trustStore);
74         }
75         catch (Exception JavaDoc e) {
76             Log.error("SSLConfig startup problem.\n" +
77                     " storeType: [" + storeType + "]\n" +
78                     " keyStoreLocation: [" + keyStoreLocation + "]\n" +
79                     " keypass: [" + keypass + "]\n" +
80                     " trustStoreLocation: [" + trustStoreLocation+ "]\n" +
81                     " trustpass: [" + trustpass + "]", e);
82             keyStore = null;
83             trustStore = null;
84             sslFactory = null;
85         }
86     }
87
88     public static String JavaDoc getKeyPassword() {
89         return keypass;
90     }
91
92     public static String JavaDoc getTrustPassword() {
93         return trustpass;
94     }
95
96     public static String JavaDoc[] getDefaultCipherSuites() {
97         String JavaDoc[] suites;
98         if (sslFactory == null) {
99             suites = new String JavaDoc[]{};
100         }
101         else {
102             suites = sslFactory.getDefaultCipherSuites();
103         }
104         return suites;
105     }
106
107     public static String JavaDoc[] getSpportedCipherSuites() {
108         String JavaDoc[] suites;
109         if (sslFactory == null) {
110             suites = new String JavaDoc[]{};
111         }
112         else {
113             suites = sslFactory.getSupportedCipherSuites();
114         }
115         return suites;
116     }
117
118     public static KeyStore JavaDoc getKeyStore() throws IOException JavaDoc {
119         if (keyStore == null) {
120             throw new IOException JavaDoc();
121         }
122         return keyStore;
123     }
124
125     public static KeyStore JavaDoc getTrustStore() throws IOException JavaDoc {
126         if (trustStore == null) {
127             throw new IOException JavaDoc();
128         }
129         return trustStore;
130     }
131
132     public static void saveStores() throws IOException JavaDoc {
133         try {
134             keyStore.store(new FileOutputStream JavaDoc(keyStoreLocation), keypass.toCharArray());
135             trustStore.store(new FileOutputStream JavaDoc(trustStoreLocation), trustpass.toCharArray());
136         }
137         catch (IOException JavaDoc e) {
138             throw e;
139         }
140         catch (Exception JavaDoc e) {
141             throw new IOException JavaDoc(e.getMessage());
142         }
143     }
144
145     public static ServerSocket JavaDoc createServerSocket(int port, InetAddress JavaDoc ifAddress) throws
146             IOException JavaDoc {
147         if (sslFactory == null) {
148             throw new IOException JavaDoc();
149         }
150         else {
151             return sslFactory.createServerSocket(port, -1, ifAddress);
152         }
153     }
154 }
Popular Tags