2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22
package org.jboss.remoting.marshal.encryption;
24
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
25
import org.jboss.logging.Logger;
27
import javax.crypto.Cipher;
28
import javax.crypto.spec.IvParameterSpec;
29
import java.io.InputStream;
30
import java.io.ObjectInput;
31
import java.io.ObjectInputStream;
32
import java.security.AccessController;
33
import java.security.Key;
34
import java.security.PrivilegedAction;
37
//$Id: EncryptionManager.java 3836 2008-04-02 03:56:30Z ron.sigal@jboss.com $
40
* Manager that deals with the generation of the Cipher
42
* ECB: Electronic Codebook Mode (NIST FIPS PUB 81)
43
* CBC: Cipher Block Chaining Mode(NIST FIPS PUB 81)
44
* PCBC: Plaintext Cipher Block Chaining (Kerberos)
45
* CFB: Cipher Feedback Mode (NIST FIPS PUB 81)
46
* OFB: Output Feedback Mode (NIST FIPS PUB 81)
48
* NoPadding: No padding.
49
PKCS5Padding: RSA, "PKCS #5: Password-Based Encryption Standard,"
50
version 1.5, Nov 1993.
51
* @author <a href="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
53
* @version $Revision: 3836 $
55
public class EncryptionManager
57
private static Logger log = Logger.getLogger(EncryptionManager.class);
58
private static Map keys = new ConcurrentHashMap();
60
private static byte[] salt8 = {
61
(byte)0x7e, (byte)0xee, (byte)0xc8, (byte)0xc7,
62
(byte)0x99, (byte)0x73, (byte)0x21, (byte)0x8c};
64
private static byte[] salt16 = {
65
(byte)0x7e, (byte)0xee, (byte)0xc8, (byte)0xc7,
66
(byte)0x99, (byte)0x73, (byte)0x21, (byte)0x8c,
67
(byte)0x7e, (byte)0xee, (byte)0xc8, (byte)0xc7,
68
(byte)0x99, (byte)0x73, (byte)0x21, (byte)0x8c};
70
private static IvParameterSpec iv8 = new IvParameterSpec(salt8);
72
private static IvParameterSpec iv16 = new IvParameterSpec(salt16);
75
public static final String TRIPLEDES = "DESede";
76
public static final String DES = "DES";
77
public static final String AES = "AES";
78
public static final String BLOWFISH = "Blowfish";
79
public static final String RC4 = "RC4";
81
public static final String DEFAULT_CIPHER_ALGORITHM = DES;
85
//Generate Keys for the common algorithms
88
keys.put("AES", loadKey(AES));
89
keys.put("DES", loadKey(DES));
90
keys.put("DESede", loadKey(TRIPLEDES));
91
keys.put("Blowfish", loadKey(BLOWFISH));
92
keys.put("RC4", loadKey(RC4));
96
if(log.isTraceEnabled())
97
log.trace("Exception in loading key",e);
103
* @param mode Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE (Wrap/Unwrap not supported)
104
* @param algo Cipher Algorithm
107
public static Cipher getCipher(int mode, String algo)
110
algo = DEFAULT_CIPHER_ALGORITHM;
111
Cipher cipher = null;
112
boolean correctMode = (mode == Cipher.ENCRYPT_MODE
113
|| mode == Cipher.DECRYPT_MODE);
115
throw new IllegalArgumentException("Cipher Mode is wrong");
119
cipher = Cipher.getInstance(algo);
120
Key key = (Key)keys.get(canonicalize(algo));
122
throw new IllegalStateException("Key is null for algo="+algo);
123
initializeCipher(cipher,key,algo,mode);
127
log.error("getCipher failed", e);
133
* Obtain an initialized cipher given the Cipher mode,
135
* @param mode Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
138
* @return initialized cipher
140
public static Cipher getCipher(int mode, String algo, Key key)
142
Cipher cipher = null;
143
boolean correctMode = (mode == Cipher.ENCRYPT_MODE
144
|| mode == Cipher.DECRYPT_MODE);
146
throw new IllegalArgumentException("Cipher Mode is wrong");
150
cipher = Cipher.getInstance(algo);
151
initializeCipher(cipher,key,algo,mode);
155
if(log.isTraceEnabled())
156
log.trace("getCipher failed:", e);
162
* Load the serialized key
167
private static Key loadKey(String algo) throws Exception
169
ClassLoader tcl = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
173
return Thread.currentThread().getContextClassLoader();
176
String file = "org/jboss/remoting/marshall/encryption/"+algo+".key";
177
InputStream is = tcl.getResourceAsStream(file);
179
throw new IllegalStateException("Key file is not locatable");
180
ObjectInput out = new ObjectInputStream(is);
181
Key key = (Key)out.readObject();
186
//Remove padding etc from the key algo
187
private static String canonicalize(String algo)
190
throw new IllegalArgumentException("Null algorithm passed");
191
String result = algo;
192
if(algo.indexOf("/")> 0)
194
result = algo.substring(0,algo.indexOf("/"));
200
* Initialize the Cipher
201
* @param cipher Cipher
203
* @param algo Algorithm
204
* @param mode Cipher Mode
207
private static void initializeCipher(Cipher cipher, Key key, String algo, int mode)
210
//No Padding required
211
if(algo.equals("AES") || algo.equals("DES") || algo.equals("DESede") ||
212
algo.equals("RC4") || algo.equals("Blowfish"))
213
cipher.init(mode, key);
215
if(algo.indexOf("AES") == 0 && algo.indexOf("AES/ECB") < 0 )
216
cipher.init(mode, key,iv16);
218
if(algo.indexOf("/CBC/") > 0 || algo.indexOf("/OFB/") > 0 ||
219
algo.indexOf("/PCBC/") > 0 || algo.indexOf("/CFB/") > 0)
220
cipher.init(mode, key,iv8);
222
cipher.init(mode, key);