View Javadoc

1   /*
2    * $Id: KeyBasedEncryptionStrategy.java 7976 2007-08-21 14:26:13Z 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.extras.pgp;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.umo.UMOEncryptionStrategy;
15  import org.mule.umo.lifecycle.InitialisationException;
16  import org.mule.umo.security.CryptoFailureException;
17  
18  import java.io.ByteArrayInputStream;
19  import java.util.Collection;
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  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  public class KeyBasedEncryptionStrategy implements UMOEncryptionStrategy
33  {
34      /**
35       * logger used by this class
36       */
37      protected static final Log logger = LogFactory.getLog(KeyBasedEncryptionStrategy.class);
38  
39      private PGPKeyRing keyManager;
40  
41      public byte[] encrypt(byte[] data, Object cryptInfo) throws CryptoFailureException
42      {
43          try
44          {
45              PGPCryptInfo pgpCryptInfo = (PGPCryptInfo)cryptInfo;
46              KeyBundle publicKey = pgpCryptInfo.getKeyBundle();
47  
48              LiteralMessageBuilder lmb = LiteralMessageBuilder.getInstance("OpenPGP");
49  
50              lmb.init(data);
51  
52              Message msg = lmb.build();
53  
54              if (pgpCryptInfo.isSignRequested())
55              {
56                  SignedMessageBuilder smb = SignedMessageBuilder.getInstance("OpenPGP");
57  
58                  smb.init(msg);
59                  smb.addSigner(keyManager.getSecretKeyBundle(), keyManager.getSecretPassphrase().toCharArray());
60  
61                  msg = smb.build();
62              }
63  
64              EncryptedMessageBuilder emb = EncryptedMessageBuilder.getInstance("OpenPGP");
65              emb.init(msg);
66              emb.addRecipient(publicKey);
67              msg = emb.build();
68  
69              return new PGPArmouredMessage(msg).getEncoded();
70          }
71          catch (Exception e)
72          {
73              throw new CryptoFailureException(this, e);
74          }
75      }
76  
77      public byte[] decrypt(byte[] data, Object cryptInfo) throws CryptoFailureException
78      {
79          try
80          {
81              MessageFactory mf = MessageFactory.getInstance("OpenPGP");
82  
83              ByteArrayInputStream in = new ByteArrayInputStream(data);
84  
85              Collection msgs = mf.generateMessages(in);
86  
87              Message msg = (Message)msgs.iterator().next();
88  
89              if (msg instanceof EncryptedMessage)
90              {
91                  msg = ((EncryptedMessage)msg).decrypt(keyManager.getSecretKeyBundle(),
92                      keyManager.getSecretPassphrase().toCharArray());
93  
94                  return new PGPArmouredMessage(msg).getEncoded();
95              }
96          }
97          catch (Exception e)
98          {
99              throw new CryptoFailureException(this, e);
100         }
101 
102         return data;
103     }
104 
105     public void initialise() throws InitialisationException
106     {
107         try
108         {
109             java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
110             java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
111         }
112         catch (Exception e)
113         {
114             throw new InitialisationException(
115                 CoreMessages.failedToCreate("KeyBasedEncryptionStrategy"), e, this);
116         }
117     }
118 
119     public PGPKeyRing getKeyManager()
120     {
121         return keyManager;
122     }
123 
124     public void setKeyManager(PGPKeyRing keyManager)
125     {
126         this.keyManager = keyManager;
127     }
128 }