View Javadoc

1   /*
2    * $Id: AbstractJCEEncryptionStrategy.java 20310 2010-11-24 10:40:35Z esteban.robles $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.security;
12  
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.security.CryptoFailureException;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.util.Base64;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.security.GeneralSecurityException;
22  import java.security.spec.AlgorithmParameterSpec;
23  import java.security.spec.KeySpec;
24  
25  import javax.crypto.Cipher;
26  import javax.crypto.SecretKey;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * A JCE based encryption strategy. It also provides base64 encoding of
34   * encrypted/decrypted data by setting the base64encoding attribute.
35   */
36  public abstract class AbstractJCEEncryptionStrategy extends AbstractNamedEncryptionStrategy
37  {
38      /**
39       * logger used by this class
40       */
41      protected transient Log logger = LogFactory.getLog(getClass());
42  
43      protected KeySpec keySpec;
44      protected SecretKey secretKey;
45      protected Cipher encryptCipher;
46      protected Cipher decryptCipher;
47  
48      protected String algorithm = null;
49  
50      protected boolean base64Encoding = true;
51  
52      public void initialise() throws InitialisationException
53      {
54          if (algorithm == null)
55          {
56              throw new InitialisationException(CoreMessages.objectIsNull("Algorithm"), this);
57          }
58          else
59          {
60              logger.debug("Using encryption algorithm: " + algorithm);
61          }
62  
63          keySpec = createKeySpec();
64  
65          try
66          {
67              secretKey = getSecretKey();
68              // Create Ciphers
69              encryptCipher = Cipher.getInstance(getAlgorithm());
70              decryptCipher = Cipher.getInstance(getAlgorithm());
71  
72              AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
73              if (paramSpec != null)
74              {
75                  encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
76                  decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
77              }
78              else
79              {
80                  encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
81                  decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
82              }
83  
84          }
85          catch (Exception e)
86          {
87              throw new InitialisationException(CoreMessages.failedToCreate("encryption ciphers"),
88                  e, this);
89          }
90      }
91  
92      protected abstract SecretKey getSecretKey() throws GeneralSecurityException;
93  
94      public InputStream encrypt(InputStream data, Object info) throws CryptoFailureException {
95          try
96          {
97              return new ByteArrayInputStream(this.encrypt(IOUtils.toByteArray(data), info));
98          }
99          catch (IOException e)
100         {
101             throw new CryptoFailureException(this, e);
102         }
103     }
104 
105     public InputStream decrypt(InputStream data, Object info) throws CryptoFailureException {
106         try
107         {
108             return new ByteArrayInputStream(this.decrypt(IOUtils.toByteArray(data), info));
109         }
110         catch (IOException e)
111         {
112             throw new CryptoFailureException(this, e);
113         }
114     }
115     
116     public byte[] encrypt(byte[] data, Object info) throws CryptoFailureException
117     {
118         try
119         {
120             byte[] buf = encryptCipher.doFinal(data);
121             if (base64Encoding)
122             {
123                 return Base64.encodeBytes(buf).getBytes();
124             }
125             else
126             {
127                 return buf;
128             }
129         }
130         catch (Exception e)
131         {
132             throw new CryptoFailureException(this, e);
133         }
134     }
135 
136     public byte[] decrypt(byte[] data, Object info) throws CryptoFailureException
137     {
138         try
139         {
140             byte[] dec = data;
141             if (base64Encoding)
142             {
143                 dec = Base64.decode(new String(data));
144             }
145             return decryptCipher.doFinal(dec);
146         }
147         catch (Exception e)
148         {
149             throw new CryptoFailureException(this, e);
150         }
151     }
152 
153     public String getAlgorithm()
154     {
155         return algorithm;
156     }
157 
158     public void setAlgorithm(String algorithm)
159     {
160         this.algorithm = algorithm;
161     }
162 
163     public String toString()
164     {
165         StringBuffer buf = new StringBuffer();
166         buf.append("Algorithm=").append(algorithm);
167         return buf.toString();
168     }
169 
170     public boolean isBase64Encoding()
171     {
172         return base64Encoding;
173     }
174 
175     public void setBase64Encoding(boolean base64Encoding)
176     {
177         this.base64Encoding = base64Encoding;
178     }
179 
180     protected abstract KeySpec createKeySpec();
181 
182     protected abstract AlgorithmParameterSpec createAlgorithmParameterSpec();
183 
184 }