KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > security > CryptoManager


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.security;
66
67 import com.jcorporate.expresso.core.misc.ByteArrayCounter;
68 import com.jcorporate.expresso.core.misc.ConfigManager;
69 import com.jcorporate.expresso.kernel.ComponentLifecycle;
70 import com.jcorporate.expresso.kernel.Configuration;
71 import com.jcorporate.expresso.kernel.exception.ChainedException;
72 import com.jcorporate.expresso.kernel.exception.ConfigurationException;
73
74
75 /**
76  * CryptoManager.java
77  * <p/>
78  * Singleton Class that acts as a facade for managing if strong or weak crypto
79  * is desired.
80  * </p>
81  * Copyright 2000-2002 Jcorporate Ltd.
82  *
83  * @author Michael Rimov
84  * @since Expresso 3.0
85  */

86 public class CryptoManager extends com.jcorporate.expresso.kernel.ComponentBase implements ComponentLifecycle {
87
88     //static final private String defaultPW = "Jcorporate Rocks";
89
static final private String JavaDoc thisClass = "com.jcorporate.expresso.core.security.CryptoManager";
90     static private CryptoManager theManager = null;
91     static protected ByteArrayCounter ivCounter = new ByteArrayCounter(8);
92     protected AbstractRandomNumber randomGenerator = null;
93     protected AbstractStringEncryption stringEncryptor = null;
94     protected StringHash stringHash = null;
95     boolean initialized;
96
97     String JavaDoc encryptMode;
98
99     /**
100      * Flag for string cryptography
101      */

102     boolean strongCrypto = false;
103
104     /**
105      * Passphrase
106      */

107     private String JavaDoc cryptoKey;
108     private String JavaDoc randomSeed;
109
110     /**
111      * Do not call this constructor directly. Use getInstance() instead.
112      * This is public ONLY so that it can be instantiated by the test program.
113      */

114     public CryptoManager() {
115     } /* CryptoManager() */
116
117     /**
118      * Singleton Generator. Call to get an instance of the Crypto Manager<p>
119      * <B>Please Note</B> If strong encryption is used, this class may take quite
120      * some time in initializing due to the nature of generating a default seed for
121      * the crypto-strength random number generator.
122      *
123      * @return the ony and only instance of the Crypto Manager;
124      * @throws ChainedException If there is a problem instantiating the crypto classes
125      */

126     public static synchronized CryptoManager getInstance()
127             throws ChainedException {
128         if (theManager == null) {
129             theManager = new CryptoManager();
130             boolean strongCrypto = false;
131
132             strongCrypto = ConfigManager.getConfig().strongCrypto();
133
134             theManager.setEncryptMode(ConfigManager.getConfig().getEncryptMode());
135             theManager.setCryptoKey(ConfigManager.getConfig().getCryptoKey());
136             theManager.setRandomSeed(ConfigManager.getConfigDir());
137             theManager.setStrongCrypto(strongCrypto);
138
139             //Do we use weak obfuscation methods?
140
if (!strongCrypto) {
141                 theManager.loadClasses(false);
142             } else {
143                 theManager.loadClasses(true);
144             }
145         }
146
147         return theManager;
148     } /* getInstance() */
149
150
151     /**
152      * Method removes all static entries.
153      */

154     public synchronized void destroy() {
155         stringEncryptor.destroy();
156         randomGenerator = null;
157         stringHash = null;
158         stringEncryptor = null;
159         theManager = null;
160
161     }
162
163     /**
164      * Returns whether the crypto manager is using strong cryptography or not.
165      *
166      * @return true if a strong cryptographic provider is included
167      */

168     public synchronized boolean isUsingStrongCrypto() {
169         return strongCrypto;
170     }
171
172     /**
173      * Returns an instantiated subclass of AbstractRandomNumber depending if
174      * strong or weak crypto is desired.
175      *
176      * @return a random number generator
177      * @see com.jcorporate.expresso.core.security.AbstractRandomNumber
178      * @see com.jcorporate.expresso.core.security.weakencryption.RandomNumber
179      * @see com.jcorporate.expresso.core.security.strongencryption.RandomNumber
180      */

181     public synchronized AbstractRandomNumber getRandomGenerator() {
182         return randomGenerator;
183     } /* getRandomGenerator() */
184
185     /**
186      * Factory method that returns an instantiated subclass of
187      * <code>AbstractStringEncryption</code> depending of strong
188      * or weak crypto is desired.
189      *
190      * @return A string encryption concrete class.
191      * @see com.jcorporate.expresso.core.security.AbstractStringEncryption
192      * @see com.jcorporate.expresso.core.security.weakencryption.StringEncryption
193      * @see com.jcorporate.expresso.core.security.strongencryption.StringEncryption
194      */

195     public synchronized AbstractStringEncryption getStringEncryption() {
196         return stringEncryptor;
197     } /* getStringEncryption() */
198
199     /**
200      * Factory method that returns an instantiated subclass of
201      * <code>AbstractStringHash</code> depending of strong
202      * or weak crypto is desired.
203      *
204      * @return An instantiated String Hashing class
205      * @see com.jcorporate.expresso.core.security.StringHash
206      */

207     public synchronized StringHash getStringHash() {
208         return stringHash;
209     } /* getStringHash() */
210
211     /**
212      * Loads the appropriate crypto classes. Also is split off from the constructor
213      * for testing purposes.
214      *
215      * @param useStrongCrypto Set to true if we desire to load the strong
216      * cryptographic classes.
217      * @throws ChainedException upon error loading the classes.
218      */

219     public synchronized void loadClasses(boolean useStrongCrypto)
220             throws ChainedException {
221         String JavaDoc packageName;
222         final String JavaDoc myName = thisClass + ".loadClasses";
223         strongCrypto = useStrongCrypto;
224
225         //Do we use weak obfuscation methods instead?
226
if (useStrongCrypto == false) {
227             packageName = "com.jcorporate.expresso.core.security.weakencryption";
228         } else {
229
230             //No
231
packageName = "com.jcorporate.expresso.core.security.strongencryption";
232         }
233         //Load up the classes
234
try {
235             randomGenerator = (AbstractRandomNumber) Class.forName(packageName +
236                     ".RandomNumber").newInstance();
237             stringEncryptor = (AbstractStringEncryption) Class.forName(packageName +
238                     ".StringEncryption").newInstance();
239             stringHash = new StringHash();
240
241             randomGenerator.setCryptoManager(this);
242             stringEncryptor.setCryptoManager(this);
243             stringHash.setCryptoManager(this);
244             stringEncryptor.init();
245             randomGenerator.init();
246         } catch (ClassNotFoundException JavaDoc ex) {
247             throw new ChainedException(myName +
248                     " Unable to load crypto class in package " +
249                     packageName, ex);
250         } catch (IllegalAccessException JavaDoc ex) {
251             throw new ChainedException(myName + ":Package " + packageName, ex);
252         } catch (InstantiationException JavaDoc ex) {
253             throw new ChainedException(myName +
254                     " Unable to instantiate a cryto class in package " +
255                     packageName, ex);
256         } catch (ChainedException e) {
257             throw e;
258         } catch (Exception JavaDoc e) {
259             throw new ChainedException(myName +
260                     ":Unable to load classes for crypto package " +
261                     packageName, e);
262         }
263     }
264
265     public synchronized void initialize() {
266         theManager = this;
267     }
268
269     /**
270      * Configure the Cryptographic manager
271      *
272      * @param newConfig the new configuration bean to configure with
273      * @throws ConfigurationException upon configuration error
274      */

275     public synchronized void configure(Configuration newConfig) throws ConfigurationException {
276         Boolean JavaDoc strongCrypto = (Boolean JavaDoc) newConfig.get("StrongCrypto");
277         this.setStrongCrypto(strongCrypto.booleanValue());
278         this.setEncryptMode((String JavaDoc) newConfig.get("EncryptMode"));
279         this.setCryptoKey((String JavaDoc) newConfig.get("CryptoKey"));
280         this.setRandomSeed((String JavaDoc) newConfig.get("RandomSeed"));
281
282
283         //Do we use weak obfuscation methods?
284
try {
285             if (!strongCrypto.booleanValue()) {
286                 theManager.loadClasses(false);
287             } else {
288                 theManager.loadClasses(true);
289             }
290         } catch (ChainedException ex) {
291             throw new ConfigurationException("Error loading cryptographic manager.", ex);
292         }
293     }
294
295     /**
296      * Reconfigure lifecycle event. Destroys itself and reconstructs with
297      * the new configuration
298      *
299      * @param newConfig the configuration data to reconfigure with.
300      */

301     public synchronized void reconfigure(Configuration newConfig) throws ConfigurationException {
302         destroy();
303         setStrongCrypto(false);
304         setEncryptMode(null);
305         setCryptoKey(null);
306         setRandomSeed(null);
307         configure(newConfig);
308     }
309
310     public synchronized boolean isStrongCrypto() {
311         return strongCrypto;
312     }
313
314     public synchronized void setStrongCrypto(boolean strongCrypto) {
315         this.strongCrypto = strongCrypto;
316     }
317
318     public synchronized String JavaDoc getEncryptMode() {
319         return encryptMode;
320     }
321
322     public synchronized void setEncryptMode(String JavaDoc encryptMode) {
323         this.encryptMode = encryptMode;
324     }
325
326     public boolean isInitialized() {
327         return initialized;
328     }
329
330     public void setCryptoKey(String JavaDoc cryptoKey) {
331         this.cryptoKey = cryptoKey;
332     }
333
334     public String JavaDoc getCryptoKey() {
335         return cryptoKey;
336     }
337
338     public void setRandomSeed(String JavaDoc randomSeed) {
339         this.randomSeed = randomSeed;
340     }
341
342     public String JavaDoc getRandomSeed() {
343         return randomSeed;
344     }
345
346
347 } /* CryptoManager */
Popular Tags