View Javadoc

1   /*
2    * $Id: KeyBasedEncryptionStrategy.java 11517 2008-03-31 21:34:19Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.module.pgp;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.lifecycle.InitialisationException;
16  import org.mule.api.security.CredentialsAccessor;
17  import org.mule.api.security.CryptoFailureException;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.security.AbstractNamedEncryptionStrategy;
20  
21  import cryptix.message.EncryptedMessage;
22  import cryptix.message.EncryptedMessageBuilder;
23  import cryptix.message.LiteralMessageBuilder;
24  import cryptix.message.Message;
25  import cryptix.message.MessageFactory;
26  import cryptix.message.SignedMessageBuilder;
27  import cryptix.openpgp.PGPArmouredMessage;
28  import cryptix.pki.KeyBundle;
29  
30  import java.io.ByteArrayInputStream;
31  import java.util.Collection;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  public class KeyBasedEncryptionStrategy extends AbstractNamedEncryptionStrategy
37  {
38      /**
39       * logger used by this class
40       */
41      protected static final Log logger = LogFactory.getLog(KeyBasedEncryptionStrategy.class);
42  
43      private PGPKeyRing keyManager;
44      private CredentialsAccessor credentialsAccessor;
45  
46      public byte[] encrypt(byte[] data, Object cryptInfo) throws CryptoFailureException
47      {
48          try
49          {
50          	PGPCryptInfo pgpCryptInfo;
51              KeyBundle publicKey;
52              
53              if (cryptInfo == null)
54              {
55                  MuleEvent event = RequestContext.getEvent();
56                  publicKey = keyManager.getKeyBundle((String)credentialsAccessor.getCredentials(
57                      event));
58                  
59                  pgpCryptInfo = new PGPCryptInfo(publicKey, false);
60              }
61              else
62              {
63                  pgpCryptInfo = (PGPCryptInfo)cryptInfo;
64                  publicKey = pgpCryptInfo.getKeyBundle();
65              }
66  
67              LiteralMessageBuilder lmb = LiteralMessageBuilder.getInstance("OpenPGP");
68  
69              lmb.init(data);
70  
71              Message msg = lmb.build();
72  
73              if (pgpCryptInfo.isSignRequested())
74              {
75                  SignedMessageBuilder smb = SignedMessageBuilder.getInstance("OpenPGP");
76  
77                  smb.init(msg);
78                  smb.addSigner(keyManager.getSecretKeyBundle(), keyManager.getSecretPassphrase().toCharArray());
79  
80                  msg = smb.build();
81              }
82  
83              EncryptedMessageBuilder emb = EncryptedMessageBuilder.getInstance("OpenPGP");
84              emb.init(msg);
85              emb.addRecipient(publicKey);
86              msg = emb.build();
87  
88              return new PGPArmouredMessage(msg).getEncoded();
89          }
90          catch (Exception e)
91          {
92              throw new CryptoFailureException(this, e);
93          }
94      }
95  
96      public byte[] decrypt(byte[] data, Object cryptInfo) throws CryptoFailureException
97      {
98          try
99          {
100             MessageFactory mf = MessageFactory.getInstance("OpenPGP");
101 
102             ByteArrayInputStream in = new ByteArrayInputStream(data);
103 
104             Collection msgs = mf.generateMessages(in);
105 
106             Message msg = (Message)msgs.iterator().next();
107 
108             if (msg instanceof EncryptedMessage)
109             {
110                 msg = ((EncryptedMessage)msg).decrypt(keyManager.getSecretKeyBundle(),
111                     keyManager.getSecretPassphrase().toCharArray());
112 
113                 return new PGPArmouredMessage(msg).getEncoded();
114             }
115         }
116         catch (Exception e)
117         {
118             throw new CryptoFailureException(this, e);
119         }
120 
121         return data;
122     }
123 
124     public void initialise() throws InitialisationException
125     {
126         try
127         {
128             java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
129             java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
130         }
131         catch (Exception e)
132         {
133             throw new InitialisationException(
134                 CoreMessages.failedToCreate("KeyBasedEncryptionStrategy"), e, this);
135         }
136     }
137 
138     public PGPKeyRing getKeyManager()
139     {
140         return keyManager;
141     }
142 
143     public void setKeyManager(PGPKeyRing keyManager)
144     {
145         this.keyManager = keyManager;
146     }
147 
148 	public CredentialsAccessor getCredentialsAccessor() {
149 		return credentialsAccessor;
150 	}
151 
152 	public void setCredentialsAccessor(CredentialsAccessor credentialsAccessor) {
153 		this.credentialsAccessor = credentialsAccessor;
154 	}
155 }