Coverage Report - org.mule.security.AbstractJCEEncryptionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJCEEncryptionStrategy
0%
0/48
0%
0/8
2.538
 
 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  0
 public abstract class AbstractJCEEncryptionStrategy extends AbstractNamedEncryptionStrategy
 37  
 {
 38  
     /**
 39  
      * logger used by this class
 40  
      */
 41  0
     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  0
     protected String algorithm = null;
 49  
 
 50  0
     protected boolean base64Encoding = true;
 51  
 
 52  
     public void initialise() throws InitialisationException
 53  
     {
 54  0
         if (algorithm == null)
 55  
         {
 56  0
             throw new InitialisationException(CoreMessages.objectIsNull("Algorithm"), this);
 57  
         }
 58  
         else
 59  
         {
 60  0
             logger.debug("Using encryption algorithm: " + algorithm);
 61  
         }
 62  
 
 63  0
         keySpec = createKeySpec();
 64  
 
 65  
         try
 66  
         {
 67  0
             secretKey = getSecretKey();
 68  
             // Create Ciphers
 69  0
             encryptCipher = Cipher.getInstance(getAlgorithm());
 70  0
             decryptCipher = Cipher.getInstance(getAlgorithm());
 71  
 
 72  0
             AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
 73  0
             if (paramSpec != null)
 74  
             {
 75  0
                 encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
 76  0
                 decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
 77  
             }
 78  
             else
 79  
             {
 80  0
                 encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
 81  0
                 decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
 82  
             }
 83  
 
 84  
         }
 85  0
         catch (Exception e)
 86  
         {
 87  0
             throw new InitialisationException(CoreMessages.failedToCreate("encryption ciphers"),
 88  
                 e, this);
 89  0
         }
 90  0
     }
 91  
 
 92  
     protected abstract SecretKey getSecretKey() throws GeneralSecurityException;
 93  
 
 94  
     public InputStream encrypt(InputStream data, Object info) throws CryptoFailureException {
 95  
         try
 96  
         {
 97  0
             return new ByteArrayInputStream(this.encrypt(IOUtils.toByteArray(data), info));
 98  
         }
 99  0
         catch (IOException e)
 100  
         {
 101  0
             throw new CryptoFailureException(this, e);
 102  
         }
 103  
     }
 104  
 
 105  
     public InputStream decrypt(InputStream data, Object info) throws CryptoFailureException {
 106  
         try
 107  
         {
 108  0
             return new ByteArrayInputStream(this.decrypt(IOUtils.toByteArray(data), info));
 109  
         }
 110  0
         catch (IOException e)
 111  
         {
 112  0
             throw new CryptoFailureException(this, e);
 113  
         }
 114  
     }
 115  
     
 116  
     public byte[] encrypt(byte[] data, Object info) throws CryptoFailureException
 117  
     {
 118  
         try
 119  
         {
 120  0
             byte[] buf = encryptCipher.doFinal(data);
 121  0
             if (base64Encoding)
 122  
             {
 123  0
                 return Base64.encodeBytes(buf).getBytes();
 124  
             }
 125  
             else
 126  
             {
 127  0
                 return buf;
 128  
             }
 129  
         }
 130  0
         catch (Exception e)
 131  
         {
 132  0
             throw new CryptoFailureException(this, e);
 133  
         }
 134  
     }
 135  
 
 136  
     public byte[] decrypt(byte[] data, Object info) throws CryptoFailureException
 137  
     {
 138  
         try
 139  
         {
 140  0
             byte[] dec = data;
 141  0
             if (base64Encoding)
 142  
             {
 143  0
                 dec = Base64.decode(new String(data));
 144  
             }
 145  0
             return decryptCipher.doFinal(dec);
 146  
         }
 147  0
         catch (Exception e)
 148  
         {
 149  0
             throw new CryptoFailureException(this, e);
 150  
         }
 151  
     }
 152  
 
 153  
     public String getAlgorithm()
 154  
     {
 155  0
         return algorithm;
 156  
     }
 157  
 
 158  
     public void setAlgorithm(String algorithm)
 159  
     {
 160  0
         this.algorithm = algorithm;
 161  0
     }
 162  
 
 163  
     public String toString()
 164  
     {
 165  0
         StringBuffer buf = new StringBuffer();
 166  0
         buf.append("Algorithm=").append(algorithm);
 167  0
         return buf.toString();
 168  
     }
 169  
 
 170  
     public boolean isBase64Encoding()
 171  
     {
 172  0
         return base64Encoding;
 173  
     }
 174  
 
 175  
     public void setBase64Encoding(boolean base64Encoding)
 176  
     {
 177  0
         this.base64Encoding = base64Encoding;
 178  0
     }
 179  
 
 180  
     protected abstract KeySpec createKeySpec();
 181  
 
 182  
     protected abstract AlgorithmParameterSpec createAlgorithmParameterSpec();
 183  
 
 184  
 }