~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/marshal/encryption/EncryptionManager.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
  *
 
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.
 
11
  *
 
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.
 
16
  *
 
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.
 
21
  */
 
22
package org.jboss.remoting.marshal.encryption;
 
23
 
 
24
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
 
25
import org.jboss.logging.Logger;
 
26
 
 
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;
 
35
import java.util.Map;
 
36
 
 
37
//$Id: EncryptionManager.java 3836 2008-04-02 03:56:30Z ron.sigal@jboss.com $
 
38
 
 
39
/**
 
40
 *  Manager that deals with the generation of the Cipher
 
41
 *  Mode:
 
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)
 
47
 *  Padding:
 
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>
 
52
 *  @since  Aug 11, 2006
 
53
 *  @version $Revision: 3836 $
 
54
 */
 
55
public class EncryptionManager
 
56
{
 
57
   private static Logger log = Logger.getLogger(EncryptionManager.class);
 
58
   private static Map keys =  new ConcurrentHashMap();
 
59
 
 
60
   private static byte[] salt8 = {
 
61
      (byte)0x7e, (byte)0xee, (byte)0xc8, (byte)0xc7,
 
62
      (byte)0x99, (byte)0x73, (byte)0x21, (byte)0x8c};
 
63
 
 
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};
 
69
 
 
70
   private static IvParameterSpec iv8 = new IvParameterSpec(salt8);
 
71
 
 
72
   private static IvParameterSpec iv16 = new IvParameterSpec(salt16);
 
73
 
 
74
   
 
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"; 
 
80
 
 
81
   public static final String DEFAULT_CIPHER_ALGORITHM = DES;
 
82
 
 
83
   static
 
84
   {
 
85
      //Generate Keys for the common algorithms
 
86
      try
 
87
      {
 
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));
 
93
      }
 
94
      catch (Exception e)
 
95
      {
 
96
         if(log.isTraceEnabled())
 
97
            log.trace("Exception in loading key",e);
 
98
      }
 
99
   }
 
100
 
 
101
   /**
 
102
    * Generate a Cipher
 
103
    * @param mode Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE (Wrap/Unwrap not supported)
 
104
    * @param algo Cipher Algorithm
 
105
    * @return cipher
 
106
    */
 
107
   public static Cipher getCipher(int mode, String algo)
 
108
   {
 
109
      if(algo == null)
 
110
         algo = DEFAULT_CIPHER_ALGORITHM;
 
111
      Cipher cipher = null;
 
112
      boolean correctMode = (mode == Cipher.ENCRYPT_MODE
 
113
                || mode == Cipher.DECRYPT_MODE);
 
114
      if(!correctMode)
 
115
         throw new IllegalArgumentException("Cipher Mode is wrong");
 
116
 
 
117
       try
 
118
      {
 
119
         cipher = Cipher.getInstance(algo);
 
120
         Key key = (Key)keys.get(canonicalize(algo));
 
121
         if(key == null)
 
122
            throw new IllegalStateException("Key is null for algo="+algo);
 
123
         initializeCipher(cipher,key,algo,mode);
 
124
      }
 
125
      catch (Throwable e)
 
126
      {
 
127
         log.error("getCipher failed", e);
 
128
      }
 
129
       return cipher;
 
130
   }
 
131
 
 
132
   /**
 
133
    * Obtain an initialized cipher given the Cipher mode,
 
134
    * algorithm and key
 
135
    * @param mode Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
 
136
    * @param algo
 
137
    * @param key
 
138
    * @return initialized cipher
 
139
    */
 
140
   public static Cipher getCipher(int mode, String algo, Key key)
 
141
   {
 
142
      Cipher cipher = null;
 
143
      boolean correctMode = (mode == Cipher.ENCRYPT_MODE
 
144
                || mode == Cipher.DECRYPT_MODE);
 
145
      if(!correctMode)
 
146
         throw new IllegalArgumentException("Cipher Mode is wrong");
 
147
 
 
148
       try
 
149
      {
 
150
         cipher = Cipher.getInstance(algo);
 
151
         initializeCipher(cipher,key,algo,mode);
 
152
      }
 
153
      catch (Throwable e)
 
154
      {
 
155
         if(log.isTraceEnabled())
 
156
            log.trace("getCipher failed:", e);
 
157
      }
 
158
       return cipher;
 
159
   }
 
160
 
 
161
   /**
 
162
    * Load the serialized key
 
163
    * @param algo
 
164
    * @return
 
165
    * @throws Exception
 
166
    */
 
167
   private static Key loadKey(String algo) throws Exception
 
168
   {
 
169
      ClassLoader tcl = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
 
170
      {
 
171
         public Object run()
 
172
         {
 
173
            return Thread.currentThread().getContextClassLoader();
 
174
         }
 
175
      });
 
176
      String file = "org/jboss/remoting/marshall/encryption/"+algo+".key";
 
177
      InputStream is = tcl.getResourceAsStream(file);
 
178
      if(is == null)
 
179
         throw new IllegalStateException("Key file is not locatable");
 
180
      ObjectInput out = new ObjectInputStream(is);
 
181
      Key key = (Key)out.readObject();
 
182
      out.close();
 
183
      return key;
 
184
   }
 
185
 
 
186
   //Remove padding etc from the key algo
 
187
   private static String canonicalize(String algo)
 
188
   {
 
189
      if(algo == null)
 
190
         throw new IllegalArgumentException("Null algorithm passed");
 
191
      String result = algo;
 
192
      if(algo.indexOf("/")> 0)
 
193
      {
 
194
         result = algo.substring(0,algo.indexOf("/"));
 
195
      }
 
196
      return result;
 
197
   }
 
198
 
 
199
   /**
 
200
    * Initialize the Cipher
 
201
    * @param cipher Cipher
 
202
    * @param key Key
 
203
    * @param algo Algorithm
 
204
    * @param mode Cipher Mode
 
205
    * @throws Exception
 
206
    */
 
207
   private static void initializeCipher(Cipher cipher, Key key, String algo, int mode)
 
208
   throws Exception
 
209
   {
 
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);
 
214
      else
 
215
      if(algo.indexOf("AES") == 0 && algo.indexOf("AES/ECB") < 0 )
 
216
         cipher.init(mode, key,iv16);
 
217
      else
 
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);
 
221
      else
 
222
         cipher.init(mode, key);
 
223
   }
 
224
}