1
2
3
4
5
6
7
8
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
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 }