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