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 | 0 | public class KeyBasedEncryptionStrategy implements UMOEncryptionStrategy |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | 0 | 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 | 0 | PGPCryptInfo pgpCryptInfo = (PGPCryptInfo)cryptInfo; |
46 | 0 | KeyBundle publicKey = pgpCryptInfo.getKeyBundle(); |
47 | |
|
48 | 0 | LiteralMessageBuilder lmb = LiteralMessageBuilder.getInstance("OpenPGP"); |
49 | |
|
50 | 0 | lmb.init(data); |
51 | |
|
52 | 0 | Message msg = lmb.build(); |
53 | |
|
54 | 0 | if (pgpCryptInfo.isSignRequested()) |
55 | |
{ |
56 | 0 | SignedMessageBuilder smb = SignedMessageBuilder.getInstance("OpenPGP"); |
57 | |
|
58 | 0 | smb.init(msg); |
59 | 0 | smb.addSigner(keyManager.getSecretKeyBundle(), keyManager.getSecretPassphrase().toCharArray()); |
60 | |
|
61 | 0 | msg = smb.build(); |
62 | |
} |
63 | |
|
64 | 0 | EncryptedMessageBuilder emb = EncryptedMessageBuilder.getInstance("OpenPGP"); |
65 | 0 | emb.init(msg); |
66 | 0 | emb.addRecipient(publicKey); |
67 | 0 | msg = emb.build(); |
68 | |
|
69 | 0 | return new PGPArmouredMessage(msg).getEncoded(); |
70 | |
} |
71 | 0 | catch (Exception e) |
72 | |
{ |
73 | 0 | throw new CryptoFailureException(this, e); |
74 | |
} |
75 | |
} |
76 | |
|
77 | |
public byte[] decrypt(byte[] data, Object cryptInfo) throws CryptoFailureException |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 0 | MessageFactory mf = MessageFactory.getInstance("OpenPGP"); |
82 | |
|
83 | 0 | ByteArrayInputStream in = new ByteArrayInputStream(data); |
84 | |
|
85 | 0 | Collection msgs = mf.generateMessages(in); |
86 | |
|
87 | 0 | Message msg = (Message)msgs.iterator().next(); |
88 | |
|
89 | 0 | if (msg instanceof EncryptedMessage) |
90 | |
{ |
91 | 0 | msg = ((EncryptedMessage)msg).decrypt(keyManager.getSecretKeyBundle(), |
92 | |
keyManager.getSecretPassphrase().toCharArray()); |
93 | |
|
94 | 0 | return new PGPArmouredMessage(msg).getEncoded(); |
95 | |
} |
96 | |
} |
97 | 0 | catch (Exception e) |
98 | |
{ |
99 | 0 | throw new CryptoFailureException(this, e); |
100 | 0 | } |
101 | |
|
102 | 0 | return data; |
103 | |
} |
104 | |
|
105 | |
public void initialise() throws InitialisationException |
106 | |
{ |
107 | |
try |
108 | |
{ |
109 | 0 | java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto()); |
110 | 0 | java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP()); |
111 | |
} |
112 | 0 | catch (Exception e) |
113 | |
{ |
114 | 0 | throw new InitialisationException( |
115 | |
CoreMessages.failedToCreate("KeyBasedEncryptionStrategy"), e, this); |
116 | 0 | } |
117 | 0 | } |
118 | |
|
119 | |
public PGPKeyRing getKeyManager() |
120 | |
{ |
121 | 0 | return keyManager; |
122 | |
} |
123 | |
|
124 | |
public void setKeyManager(PGPKeyRing keyManager) |
125 | |
{ |
126 | 0 | this.keyManager = keyManager; |
127 | 0 | } |
128 | |
} |