1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.pgp;
12
13 import cryptix.message.EncryptedMessageBuilder;
14 import cryptix.message.LiteralMessageBuilder;
15 import cryptix.message.Message;
16 import cryptix.message.MessageException;
17 import cryptix.message.SignedMessageBuilder;
18 import cryptix.openpgp.PGPArmouredMessage;
19 import cryptix.pki.ExtendedKeyStore;
20 import cryptix.pki.KeyBundle;
21
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.security.KeyStoreException;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.Principal;
28 import java.security.UnrecoverableKeyException;
29 import java.security.cert.CertificateException;
30 import java.util.Enumeration;
31 import java.util.Iterator;
32
33 public class GenerateTestMessage
34 {
35 private static ExtendedKeyStore clientPublicRing, clientPrivateRing;
36 private static ExtendedKeyStore serverPrivateRing;
37
38 private static KeyBundle serverPublicKey;
39 private static KeyBundle clientPrivateKey;
40
41 public static void readKeyrings()
42 {
43 clientPublicRing = readKeyRing("clientPublic.gpg");
44 clientPrivateRing = readKeyRing("clientPrivate.gpg");
45 serverPrivateRing = readKeyRing("serverPrivate.gpg");
46 }
47
48 public static ExtendedKeyStore readKeyRing(String filename)
49 {
50
51 ExtendedKeyStore ring = null;
52
53 try
54 {
55
56 FileInputStream in = new FileInputStream(filename);
57
58 ring = (ExtendedKeyStore)ExtendedKeyStore.getInstance("OpenPGP/KeyRing");
59 ring.load(in, null);
60
61 in.close();
62
63 }
64 catch (IOException ioe)
65 {
66 System.err.println("IOException... You did remember to run the "
67 + "GenerateAndWriteKey example first, right?");
68 ioe.printStackTrace();
69 System.exit(-1);
70 }
71 catch (NoSuchAlgorithmException nsae)
72 {
73 System.err.println("Cannot find the OpenPGP KeyRing. "
74 + "This usually means that the Cryptix OpenPGP provider is not "
75 + "installed correctly.");
76 nsae.printStackTrace();
77 System.exit(-1);
78 }
79 catch (KeyStoreException kse)
80 {
81 System.err.println("Reading keyring failed.");
82 kse.printStackTrace();
83 System.exit(-1);
84 }
85 catch (CertificateException ce)
86 {
87 System.err.println("Reading keyring failed.");
88 ce.printStackTrace();
89 System.exit(-1);
90 }
91
92 return ring;
93 }
94
95 public static KeyBundle findKeyBundle(ExtendedKeyStore ring, String principal) throws Exception
96 {
97
98 for (Enumeration e = ring.aliases(); e.hasMoreElements();)
99 {
100 String aliasId = (String)e.nextElement();
101 KeyBundle bundle = ring.getKeyBundle(aliasId);
102 if (bundle != null)
103 {
104 for (Iterator users = bundle.getPrincipals(); users.hasNext();)
105 {
106 Principal princ = (Principal)users.next();
107 System.out.println("aliasId:" + aliasId + ", user:" + princ.toString());
108 if (princ.toString().equals(principal))
109 {
110 return bundle;
111 }
112 }
113 }
114 }
115
116 throw new Exception("KeyBundle not found for " + principal);
117 }
118
119 public static void decodeKeyRings() throws Exception
120 {
121 serverPublicKey = findKeyBundle(clientPublicRing, "Mule server <mule_server@mule.com>");
122 clientPrivateKey = findKeyBundle(clientPrivateRing, "Mule client <mule_client@mule.com>");
123 System.out.println("Server private keyring:");
124 findKeyBundle(serverPrivateRing, "Mule server <mule_server@mule.com>");
125 }
126
127 public static void writeMsg()
128 {
129 Message msg = null;
130
131 try
132 {
133 String data = "This is a test message.\n" + "This is another line.\n";
134 LiteralMessageBuilder lmb = LiteralMessageBuilder.getInstance("OpenPGP");
135 lmb.init(data);
136 msg = lmb.build();
137 }
138 catch (NoSuchAlgorithmException nsae)
139 {
140 System.err.println("Cannot find the OpenPGP LiteralMessageBuilder."
141 + " This usually means that the Cryptix OpenPGP provider is not "
142 + "installed correctly.");
143 nsae.printStackTrace();
144 System.exit(-1);
145 }
146 catch (MessageException me)
147 {
148 System.err.println("Creating the literal message failed.");
149 me.printStackTrace();
150 System.exit(-1);
151 }
152
153
154
155
156
157
158
159 try
160 {
161
162 SignedMessageBuilder smb = SignedMessageBuilder.getInstance("OpenPGP");
163
164
165
166
167
168
169 smb.init(msg);
170 smb.addSigner(clientPrivateKey, "TestingPassphrase".toCharArray());
171
172 msg = smb.build();
173
174 }
175 catch (NoSuchAlgorithmException nsae)
176 {
177 System.err.println("Cannot find the OpenPGP SignedMessageBuilder. "
178 + "This usually means that the Cryptix OpenPGP provider is not "
179 + "installed correctly.");
180 nsae.printStackTrace();
181 System.exit(-1);
182 }
183 catch (UnrecoverableKeyException uke)
184 {
185 System.err.println("Incorrect passphrase.");
186 uke.printStackTrace();
187 System.exit(-1);
188 }
189 catch (MessageException me)
190 {
191 System.err.println("Generating the message failed.");
192 me.printStackTrace();
193 System.exit(-1);
194 }
195
196
197
198
199 try
200 {
201
202 PGPArmouredMessage armoured;
203
204 armoured = new PGPArmouredMessage(msg);
205 FileOutputStream out = new FileOutputStream("signed.asc");
206 out.write(armoured.getEncoded());
207 out.close();
208
209 }
210 catch (MessageException me)
211 {
212 System.err.println("Writing the encrypted message failed.");
213 me.printStackTrace();
214 System.exit(-1);
215 }
216 catch (IOException ioe)
217 {
218 System.err.println("Writing the encrypted message failed.");
219 ioe.printStackTrace();
220 System.exit(-1);
221 }
222
223
224
225
226 try
227 {
228
229 EncryptedMessageBuilder emb = EncryptedMessageBuilder.getInstance("OpenPGP");
230 emb.init(msg);
231 emb.addRecipient(serverPublicKey);
232 msg = emb.build();
233
234 }
235 catch (NoSuchAlgorithmException nsae)
236 {
237 System.err.println("Cannot find the OpenPGP " + "EncryptedMessageBuilder. "
238 + "This usually means that the Cryptix OpenPGP provider is not "
239 + "installed correctly.");
240 nsae.printStackTrace();
241 System.exit(-1);
242 }
243 catch (MessageException me)
244 {
245 System.err.println("Creating the encrypted message failed.");
246 me.printStackTrace();
247 System.exit(-1);
248 }
249
250
251
252
253 try
254 {
255
256 PGPArmouredMessage armoured;
257
258 armoured = new PGPArmouredMessage(msg);
259 FileOutputStream out = new FileOutputStream("encrypted-signed.asc");
260 out.write(armoured.getEncoded());
261 out.close();
262
263 }
264 catch (MessageException me)
265 {
266 System.err.println("Writing the encrypted message failed.");
267 me.printStackTrace();
268 System.exit(-1);
269 }
270 catch (IOException ioe)
271 {
272 System.err.println("Writing the encrypted message failed.");
273 ioe.printStackTrace();
274 System.exit(-1);
275 }
276
277 }
278
279 public static void main(String[] args) throws Exception
280 {
281 java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
282 java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
283
284 readKeyrings();
285 decodeKeyRings();
286
287 writeMsg();
288 }
289 }