KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > util > SSLManager


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/util/SSLManager.java,v 1.9.2.2 2004/09/23 21:46:26 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.util;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.net.HttpURLConnection JavaDoc;
25 import java.security.KeyStore JavaDoc;
26 import java.security.Provider JavaDoc;
27 import java.security.Security JavaDoc;
28
29 import javax.swing.JOptionPane JavaDoc;
30
31 import org.apache.jmeter.gui.GuiPackage;
32 import org.apache.jmeter.util.keystore.JmeterKeyStore;
33 import org.apache.jorphan.logging.LoggingManager;
34 import org.apache.log.Logger;
35
36 /**
37  * The SSLManager handles the KeyStore information for JMeter. Basically, it
38  * handles all the logic for loading and initializing all the JSSE parameters
39  * and selecting the alias to authenticate against if it is available.
40  * SSLManager will try to automatically select the client certificate for you,
41  * but if it can't make a decision, it will pop open a dialog asking you for
42  * more information.
43  *
44  * @author <a HREF="bloritsch@apache.org">Berin Loritsch</a>
45  * @version CVS $Revision: 1.9.2.2 $ $Date: 2004/09/23 21:46:26 $
46  */

47 public abstract class SSLManager
48 {
49     transient private static Logger log = LoggingManager.getLoggerForClass();
50     /** Singleton instance of the manager */
51     private static SSLManager manager;
52     private static boolean isIAIKProvider = false;
53     private static boolean isSSLSupported = false;
54     private static Provider JavaDoc sslProvider = null;
55
56     /** Cache the KeyStore instance */
57     private JmeterKeyStore keyStore;
58     /** Cache the TrustStore instance */
59     private KeyStore JavaDoc trustStore;
60     /** Have the password available */
61     protected String JavaDoc defaultpw =
62         JMeterUtils.getJMeterProperties().getProperty(
63             "javax.net.ssl.keyStorePassword");
64
65     /**
66      * Resets the SSLManager so that we can create a new one with a new keystore
67      */

68     static public void reset()
69     {
70         SSLManager.manager = null;
71     }
72
73     public abstract void setContext(HttpURLConnection JavaDoc conn);
74
75     /**
76      * Default implementation of setting the Provider
77      */

78     protected void setProvider(Provider JavaDoc provider)
79     {
80         if ( null != provider )
81         {
82             Security.addProvider( provider );
83         }
84     }
85
86
87     /**
88      * Opens and initializes the KeyStore. If the password for the KeyStore is
89      * not set, this method will prompt you to enter it. Unfortunately, there
90      * is no PasswordEntryField available from JOptionPane.
91      */

92     protected JmeterKeyStore getKeyStore()
93     {
94         String JavaDoc password = this.defaultpw;
95
96         if (null == this.keyStore)
97         {
98             String JavaDoc defaultName =
99                 JMeterUtils.getJMeterProperties().getProperty("user.home")
100                     + File.separator
101                     + ".keystore";
102             String JavaDoc fileName =
103                 JMeterUtils.getJMeterProperties().getProperty(
104                     "javax.net.ssl.keyStore",
105                     defaultName);
106             System.setProperty("javax.net.ssl.keyStore", fileName);
107
108             try
109             {
110                 if (fileName.endsWith(".p12") || fileName.endsWith(".P12"))
111                 {
112                     this.keyStore = JmeterKeyStore.getInstance("pkcs12");
113                     log.info("KeyStore Type: PKCS 12");
114                     System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
115                 }
116                 else
117                 {
118                     this.keyStore = JmeterKeyStore.getInstance("JKS");
119                     log.info("KeyStore Type: JKS");
120                 }
121             }
122             catch (Exception JavaDoc e)
123             {
124                 JOptionPane.showMessageDialog(
125                     GuiPackage.getInstance().getMainFrame(),
126                     e,
127                     JMeterUtils.getResString("ssl_error_title"),
128                     JOptionPane.ERROR_MESSAGE);
129                 this.keyStore = null;
130                 throw new RuntimeException JavaDoc("KeyStore Problem");
131             }
132
133             if (null == password)
134             {
135                 if (null == defaultpw)
136                 {
137                     this.defaultpw =
138                         JMeterUtils.getJMeterProperties().getProperty(
139                             "javax.net.ssl.keyStorePassword");
140
141                     if (null == defaultpw)
142                     {
143                         synchronized (this)
144                         {
145                             this.defaultpw =
146                                 JOptionPane.showInputDialog(
147                                     GuiPackage.getInstance().getMainFrame(),
148                                     JMeterUtils.getResString("ssl_pass_prompt"),
149                                     JMeterUtils.getResString("ssl_pass_title"),
150                                     JOptionPane.QUESTION_MESSAGE);
151                             JMeterUtils.getJMeterProperties().setProperty(
152                                 "javax.net.ssl.keyStorePassword",
153                                 this.defaultpw);
154                         }
155                     }
156                 }
157
158                 password = this.defaultpw;
159                 System.setProperty("javax.net.ssl.keyStorePassword", password);
160             }
161
162             try
163             {
164                 File JavaDoc initStore = new File JavaDoc(fileName);
165
166                 if (initStore.exists())
167                 {
168                     this.keyStore.load(new FileInputStream JavaDoc(initStore),password);
169                 }
170                 else
171                 {
172                     this.keyStore.load(null, password);
173                 }
174             }
175             catch (Exception JavaDoc e)
176             {
177                 log.error("Couldn't load keystore" ,e);
178             }
179
180         log.info("JmeterKeyStore Location: " + fileName);
181         log.info("JmeterKeyStore type: " + this.keyStore.getClass().toString());
182         }
183
184         return this.keyStore;
185     }
186
187     /**
188      * Opens and initializes the TrustStore.
189      */

190     protected KeyStore JavaDoc getTrustStore()
191     {
192         if (null == this.trustStore)
193         {
194             String JavaDoc fileName =
195                 JMeterUtils.getPropDefault("javax.net.ssl.trustStore", "");
196             System.setProperty("javax.net.ssl.trustStore", fileName);
197
198             try
199             {
200                 if (fileName.endsWith(".iaik"))
201                 {
202                     this.trustStore =
203                         KeyStore.getInstance("IAIKKeyStore", "IAIK");
204                 }
205                 else
206                 {
207                     this.trustStore = KeyStore.getInstance("JKS");
208                     log.info("KeyStore Type: JKS");
209                 }
210             }
211             catch (Exception JavaDoc e)
212             {
213                 JOptionPane.showMessageDialog(
214                     GuiPackage.getInstance().getMainFrame(),
215                     e,
216                     JMeterUtils.getResString("ssl_error_title"),
217                     JOptionPane.ERROR_MESSAGE);
218                 this.trustStore = null;
219                 throw new RuntimeException JavaDoc("TrustStore Problem");
220             }
221
222             try
223             {
224                 File JavaDoc initStore = new File JavaDoc(fileName);
225
226                 if (initStore.exists())
227                 {
228                     this.trustStore.load(new FileInputStream JavaDoc(initStore), null);
229                 }
230                 else
231                 {
232                     this.trustStore.load(null, null);
233                 }
234             }
235             catch (Exception JavaDoc e)
236             {
237                 throw new RuntimeException JavaDoc(
238                     "Can't load TrustStore: " + e.toString());
239             }
240
241             log.info("TrustStore Location: " + fileName);
242             log.info("TrustStore type: " + this.keyStore.getClass().toString());
243         }
244
245         return this.trustStore;
246     }
247
248     /**
249      * Protected Constructor to remove the possibility of directly instantiating
250      * this object. Create the SSLContext, and wrap all the X509KeyManagers
251      * with our X509KeyManager so that we can choose our alias.
252      */

253     protected SSLManager() {}
254
255     /**
256      * Static accessor for the SSLManager object. The SSLManager is a
257      * singleton.
258      */

259     public static final SSLManager getInstance()
260     {
261         if (null == SSLManager.manager)
262         {
263             if (SSLManager.isSSLSupported)
264             {
265                 String JavaDoc classname = null;
266                 if (SSLManager.isIAIKProvider)
267                 {
268                     classname = "org.apache.jmeter.util.IaikSSLManager";
269                 }
270                 else
271                 {
272                     classname = "org.apache.jmeter.util.JsseSSLManager";
273                 }
274
275                 try
276                 {
277                     Class JavaDoc clazz = Class.forName(classname);
278                     Constructor JavaDoc con =
279                         clazz.getConstructor(new Class JavaDoc[] { Provider JavaDoc.class });
280                     SSLManager.manager =
281                         (SSLManager) con.newInstance(
282                             new Object JavaDoc[] { SSLManager.sslProvider });
283                 }
284                 catch (Exception JavaDoc e)
285                 {
286                     log.error("",e);
287                     SSLManager.isSSLSupported = false;
288                     return null;
289                 }
290             }
291         }
292
293         return SSLManager.manager;
294     }
295
296     /**
297      * Test wether SSL is supported or not.
298      */

299     public static final boolean isSSLSupported()
300     {
301         return SSLManager.isSSLSupported;
302     }
303
304     // Moved from SSLStaticProvider so all SSL specific management is done in
305
// one place.
306
static
307     {
308         SSLManager.isSSLSupported = true;
309         SSLManager.sslProvider = null;
310 /*
311         try {
312             // Class.forName() was choking if the property wasn't set on the
313             // line below..
314             String strSSLProvider =
315                 JMeterUtils.getPropDefault("ssl.provider", null);
316             if (strSSLProvider != null)
317             {
318                 SSLManager.sslProvider =
319                     (Provider) Class.forName(strSSLProvider).newInstance();
320                 SSLManager.isSSLSupported = true;
321             }
322         } catch (Exception noSSL) {
323             log.error("",noSSL);
324         }
325
326         try {
327             if(SSLManager.sslProvider != null) {
328                 log.info("SSL Provider is: " + SSLManager.sslProvider);
329                 Security.addProvider(SSLManager.sslProvider);
330                 // register jsse provider
331             }
332         } catch (Exception ssl) {
333             // ignore
334         }
335
336         String protocol =
337             JMeterUtils.getPropDefault(
338                 "ssl.pkgs",
339                 JMeterUtils.getPropDefault("java.protocol.handler.pkgs", null));
340         SSLManager.sslProvider = null;
341         // register https protocol handler. JSSE needs a provider--but iSaSiLk
342         // does not.
343         if (null != protocol) {
344             System.setProperty("java.protocol.handler.pkgs", protocol);
345             if ("iaik.protocol".equals(protocol)) {
346                 SSLManager.isSSLSupported = true;
347                 SSLManager.isIAIKProvider = true;
348             } else {
349                 // This is the case where a provider is set and
350                 // java.protocol.handler.pkgs is set
351                 SSLManager.isSSLSupported = true;
352             }
353         } else {
354             SSLManager.isSSLSupported = true;
355         }*/

356     }
357 }
358
Popular Tags