1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | 4 | public class KeyBasedEncryptionStrategy extends AbstractNamedEncryptionStrategy |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | 2 | 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 | 0 | if (cryptInfo == null) |
54 | |
{ |
55 | 0 | MuleEvent event = RequestContext.getEvent(); |
56 | 0 | publicKey = keyManager.getKeyBundle((String)credentialsAccessor.getCredentials( |
57 | |
event)); |
58 | |
|
59 | 0 | pgpCryptInfo = new PGPCryptInfo(publicKey, false); |
60 | 0 | } |
61 | |
else |
62 | |
{ |
63 | 0 | pgpCryptInfo = (PGPCryptInfo)cryptInfo; |
64 | 0 | publicKey = pgpCryptInfo.getKeyBundle(); |
65 | |
} |
66 | |
|
67 | 0 | LiteralMessageBuilder lmb = LiteralMessageBuilder.getInstance("OpenPGP"); |
68 | |
|
69 | 0 | lmb.init(data); |
70 | |
|
71 | 0 | Message msg = lmb.build(); |
72 | |
|
73 | 0 | if (pgpCryptInfo.isSignRequested()) |
74 | |
{ |
75 | 0 | SignedMessageBuilder smb = SignedMessageBuilder.getInstance("OpenPGP"); |
76 | |
|
77 | 0 | smb.init(msg); |
78 | 0 | smb.addSigner(keyManager.getSecretKeyBundle(), keyManager.getSecretPassphrase().toCharArray()); |
79 | |
|
80 | 0 | msg = smb.build(); |
81 | |
} |
82 | |
|
83 | 0 | EncryptedMessageBuilder emb = EncryptedMessageBuilder.getInstance("OpenPGP"); |
84 | 0 | emb.init(msg); |
85 | 0 | emb.addRecipient(publicKey); |
86 | 0 | msg = emb.build(); |
87 | |
|
88 | 0 | return new PGPArmouredMessage(msg).getEncoded(); |
89 | |
} |
90 | 0 | catch (Exception e) |
91 | |
{ |
92 | 0 | throw new CryptoFailureException(this, e); |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
public byte[] decrypt(byte[] data, Object cryptInfo) throws CryptoFailureException |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 0 | MessageFactory mf = MessageFactory.getInstance("OpenPGP"); |
101 | |
|
102 | 0 | ByteArrayInputStream in = new ByteArrayInputStream(data); |
103 | |
|
104 | 0 | Collection msgs = mf.generateMessages(in); |
105 | |
|
106 | 0 | Message msg = (Message)msgs.iterator().next(); |
107 | |
|
108 | 0 | if (msg instanceof EncryptedMessage) |
109 | |
{ |
110 | 0 | msg = ((EncryptedMessage)msg).decrypt(keyManager.getSecretKeyBundle(), |
111 | |
keyManager.getSecretPassphrase().toCharArray()); |
112 | |
|
113 | 0 | return new PGPArmouredMessage(msg).getEncoded(); |
114 | |
} |
115 | |
} |
116 | 0 | catch (Exception e) |
117 | |
{ |
118 | 0 | throw new CryptoFailureException(this, e); |
119 | 0 | } |
120 | |
|
121 | 0 | return data; |
122 | |
} |
123 | |
|
124 | |
public void initialise() throws InitialisationException |
125 | |
{ |
126 | |
try |
127 | |
{ |
128 | 8 | java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto()); |
129 | 8 | java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP()); |
130 | |
} |
131 | 0 | catch (Exception e) |
132 | |
{ |
133 | 0 | throw new InitialisationException( |
134 | |
CoreMessages.failedToCreate("KeyBasedEncryptionStrategy"), e, this); |
135 | 8 | } |
136 | 8 | } |
137 | |
|
138 | |
public PGPKeyRing getKeyManager() |
139 | |
{ |
140 | 4 | return keyManager; |
141 | |
} |
142 | |
|
143 | |
public void setKeyManager(PGPKeyRing keyManager) |
144 | |
{ |
145 | 4 | this.keyManager = keyManager; |
146 | 4 | } |
147 | |
|
148 | |
public CredentialsAccessor getCredentialsAccessor() { |
149 | 0 | return credentialsAccessor; |
150 | |
} |
151 | |
|
152 | |
public void setCredentialsAccessor(CredentialsAccessor credentialsAccessor) { |
153 | 0 | this.credentialsAccessor = credentialsAccessor; |
154 | 0 | } |
155 | |
} |