KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > NewMessageCryptoInfo


1 package net.suberic.pooka.gui;
2
3 import java.security.Key JavaDoc;
4 import java.util.*;
5
6 import javax.mail.Address JavaDoc;
7 import javax.mail.Message JavaDoc;
8 import javax.mail.MessagingException JavaDoc;
9 import javax.mail.internet.MimeMessage JavaDoc;
10 import javax.mail.internet.InternetHeaders JavaDoc;
11 import javax.mail.internet.InternetAddress JavaDoc;
12
13 import net.suberic.pooka.*;
14 import net.suberic.crypto.*;
15
16 /**
17  * Encapsulates the encryption info for a new message.
18  */

19 public class NewMessageCryptoInfo extends MessageCryptoInfo {
20
21   List mAttachKeys = new LinkedList();
22
23   List mRecipientMatches = new LinkedList();
24
25   public static int CRYPTO_YES = 0;
26   public static int CRYPTO_DEFAULT = 5;
27   public static int CRYPTO_NO = 10;
28
29   // whether or not we want to encrypt this message.
30
int mEncryptMessage = CRYPTO_DEFAULT;
31
32   // whether or not we want to sign this message
33
int mSignMessage = CRYPTO_DEFAULT;
34
35   // the configured list of recipients.
36
List mRecipientInfos = new LinkedList();
37
38   /**
39    * Creates a new NewMessageCryptoInfo.
40    */

41   public NewMessageCryptoInfo(NewMessageInfo nmi) {
42     super(nmi);
43   }
44
45   // keys
46

47   // the signature key.
48
Key JavaDoc mSignatureKey = null;
49
50   // the encryption key
51
Key JavaDoc mEncryptionKey = null;
52
53   /**
54    * The Signature Key for this set of recipients.
55    */

56   public Key JavaDoc getSignatureKey() {
57     return mSignatureKey;
58   }
59
60   /**
61    * Sets the encryption key for encrypting this message.
62    */

63   public void setSignatureKey(Key JavaDoc pSignatureKey) {
64     mSignatureKey = pSignatureKey;
65   }
66
67   /**
68    * Sets the encryption key for encrypting this message.
69    */

70   public void setEncryptionKey(Key JavaDoc pEncryptionKey) {
71     mEncryptionKey = pEncryptionKey;
72   }
73
74   /**
75    * Gets the encryption key we're using for this message.
76    */

77   public Key JavaDoc getEncryptionKey() {
78     return mEncryptionKey;
79   }
80
81   // sign message.
82

83   /**
84    * Returns whether we're planning on signing this message or not.
85    */

86   public int getSignMessage() {
87     return mSignMessage;
88   }
89
90   /**
91    * Sets whether or not we want to sign this message.
92    */

93   public void setSignMessage(int pSignMessage) {
94     mSignMessage = pSignMessage;
95   }
96
97   // encrypt message.
98

99   /**
100    * Returns whether we're planning on encrypting this message or not.
101    */

102   public int getEncryptMessage() {
103     return mEncryptMessage;
104   }
105
106   /**
107    * Sets whether or not we want to encrypt this message.
108    */

109   public void setEncryptMessage(int pEncryptMessage) {
110     mEncryptMessage = pEncryptMessage;
111   }
112
113   // attach keys.
114

115   /**
116    * Attaches an encryption key to this message.
117    */

118   public synchronized void attachEncryptionKey(Key JavaDoc key) {
119     if (! mAttachKeys.contains(key))
120       mAttachKeys.add(key);
121   }
122
123   /**
124    * Attaches an encryption key to this message.
125    */

126   public synchronized void removeEncryptionKey(Key JavaDoc key) {
127     if (mAttachKeys.contains(key)) {
128       mAttachKeys.remove(key);
129     }
130
131   }
132
133   /**
134    * Returns the keys to be attached.
135    */

136   public List getAttachKeys() {
137     return new LinkedList(mAttachKeys);
138   }
139
140   // methods.
141

142   /**
143    * Creates the attached key parts for this message.
144    */

145   public List createAttachedKeyParts() {
146     LinkedList keyParts = new LinkedList();
147     List attachKeys = getAttachKeys();
148     if (attachKeys != null) {
149       for (int i = 0; i < attachKeys.size(); i++) {
150         EncryptionKey currentKey = (EncryptionKey)attachKeys.get(i);
151         try {
152           EncryptionUtils utils = currentKey.getEncryptionUtils();
153           keyParts.add(utils.createPublicKeyPart(new Key JavaDoc[] { currentKey }));
154         } catch (Exception JavaDoc e) {
155           // FIXME ignore for now.
156
System.out.println("caught exception adding key to message: " + e);
157           e.printStackTrace();
158         }
159       }
160     }
161
162     return keyParts;
163   }
164
165   /**
166    * Returns the encrypted and/or signed message(s), as appropriate.
167    */

168   public Map createEncryptedMessages(MimeMessage JavaDoc mm) throws MessagingException JavaDoc, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
169     Map returnValue = new HashMap();
170
171     List recipientInfoList = getCryptoRecipientInfos();
172     for (int i = 0; i < recipientInfoList.size(); i++) {
173       returnValue.put(((CryptoRecipientInfo) recipientInfoList.get(i)).handleMessage(mm),((CryptoRecipientInfo) recipientInfoList.get(i)).getAllRecipients() );
174     }
175
176     return returnValue;
177   }
178
179   /**
180    * Returns the configured CryptoRecipientInfos.
181    */

182   public List getCryptoRecipientInfos() {
183     return mRecipientInfos;
184   }
185
186   /**
187    * Updates the CryptoRecipientInfos with information from the
188    * MessageUI.
189    */

190   public boolean updateRecipientInfos(UserProfile profile, InternetHeaders JavaDoc headers) throws javax.mail.internet.AddressException JavaDoc, javax.mail.MessagingException JavaDoc {
191     // just use the defaults for now.
192

193     String JavaDoc toHeader = headers.getHeader("To", ",");
194     if (toHeader == null) {
195       throw new MessagingException JavaDoc(Pooka.getProperty("error.NewMessage.noTo", "No To: recipient"));
196     }
197     InternetAddress JavaDoc[] toAddresses = InternetAddress.parse(headers.getHeader("To", ","), false);
198     if (toAddresses == null || toAddresses.length == 0) {
199       throw new MessagingException JavaDoc(Pooka.getProperty("error.NewMessage.noTo", "No To: recipient"));
200     }
201
202     String JavaDoc ccHeaderLine = headers.getHeader("CC", ",");
203     InternetAddress JavaDoc[] ccAddresses;
204     if (ccHeaderLine != null && ccHeaderLine.length() > 0) {
205       ccAddresses = InternetAddress.parse(ccHeaderLine, false);
206     } else {
207       ccAddresses = new InternetAddress JavaDoc[0];
208     }
209
210     String JavaDoc bccHeaderLine = headers.getHeader("BCC", ",");
211     InternetAddress JavaDoc[] bccAddresses;
212     if (bccHeaderLine != null && bccHeaderLine.length() > 0) {
213       bccAddresses = InternetAddress.parse(bccHeaderLine, false);
214     } else {
215       bccAddresses = new InternetAddress JavaDoc[0];
216     }
217
218     Key JavaDoc cryptKey = null;
219
220     if (getEncryptMessage() != CRYPTO_NO)
221       cryptKey = getEncryptionKey();
222
223     Key JavaDoc sigKey = null;
224     if (getSignMessage() != CRYPTO_NO)
225       sigKey = getSignatureKey();
226
227     CryptoRecipientInfo info = new CryptoRecipientInfo(sigKey, cryptKey, toAddresses, ccAddresses, bccAddresses);
228
229     mRecipientInfos = new LinkedList();
230     mRecipientInfos.add(info);
231
232     return true;
233   }
234
235   // Recipient/encryption key matches.
236

237   /**
238    * This represents a match between a recipient set and an encryption
239    * configuration. The assumption is that all of the following recipients
240    * can receive the same message.
241    */

242   public class CryptoRecipientInfo {
243
244     // the signature key.
245
Key JavaDoc mSignatureKey = null;
246
247     // the encryption key
248
Key JavaDoc mEncryptionKey = null;
249
250     // the recipients
251
Address JavaDoc[] toList = null;
252     Address JavaDoc[] ccList = null;
253     Address JavaDoc[] bccList = null;
254
255     /**
256      * Creteas a new CryptoRecipieintInfo.
257      */

258     public CryptoRecipientInfo() {
259
260     }
261
262     /**
263      * Creates a new CryptoRecipieintInfo with the given signatureKey,
264      * encryptionKey, toList, ccList, and bccList.
265      */

266     public CryptoRecipientInfo(Key JavaDoc pSignatureKey, Key JavaDoc pEncryptionKey, Address JavaDoc[] pToList, Address JavaDoc[] pCcList, Address JavaDoc[] pBccList) {
267
268       setEncryptionKey(pEncryptionKey);
269       setSignatureKey(pSignatureKey);
270
271       setRecipients(pToList, Message.RecipientType.TO);
272       setRecipients(pCcList, Message.RecipientType.CC);
273       setRecipients(pBccList, Message.RecipientType.BCC);
274     }
275
276
277     /**
278      * The recipients for this crypto configuration.
279      */

280     public Address JavaDoc[] getRecipients(Message.RecipientType JavaDoc type) {
281       if (type == Message.RecipientType.TO)
282         return toList;
283       else if (type == Message.RecipientType.CC)
284         return ccList;
285       else if (type == Message.RecipientType.BCC)
286         return bccList;
287       else
288         return null;
289     }
290
291     /**
292      * The recipients for this crypto configuration.
293      */

294     public Address JavaDoc[] getAllRecipients() {
295       Address JavaDoc[] returnValue = new Address JavaDoc[0];
296       returnValue = appendToArray(returnValue, toList);
297       returnValue = appendToArray(returnValue, ccList);
298       returnValue = appendToArray(returnValue, bccList);
299
300       return returnValue;
301     }
302
303     /**
304      * Appends to an array of Addresses.
305      */

306     private Address JavaDoc[] appendToArray(Address JavaDoc[] original, Address JavaDoc[] toAdd) {
307       if (toAdd != null && toAdd.length > 0) {
308         int oldSize = original.length;
309         Address JavaDoc[] newReturnValue = new Address JavaDoc[original.length + toAdd.length];
310         System.arraycopy(original, 0, newReturnValue, 0, original.length);
311         System.arraycopy(toAdd, 0, newReturnValue, original.length, toAdd.length);
312         return newReturnValue;
313       } else {
314         return original;
315       }
316     }
317
318     /**
319      * Sets the recipients for the particular type.
320      */

321     public void setRecipients(Address JavaDoc[] pRecipients, Message.RecipientType JavaDoc type) {
322       if (type == Message.RecipientType.TO)
323         toList = pRecipients;
324       else if (type == Message.RecipientType.CC)
325         ccList = pRecipients;
326       else if (type == Message.RecipientType.BCC)
327         bccList = pRecipients;
328
329     }
330
331     /**
332      * The Signature Key for this set of recipients.
333      */

334     public Key JavaDoc getSignatureKey() {
335       return mSignatureKey;
336     }
337
338     /**
339      * Sets the encryption key for encrypting this message.
340      */

341     public void setSignatureKey(Key JavaDoc pSignatureKey) {
342       mSignatureKey = pSignatureKey;
343     }
344
345     /**
346      * Sets the encryption key for encrypting this message.
347      */

348     public void setEncryptionKey(Key JavaDoc pEncryptionKey) {
349       mEncryptionKey = pEncryptionKey;
350     }
351
352     /**
353      * Gets the encryption key we're using for this message.
354      */

355     public Key JavaDoc getEncryptionKey() {
356       return mEncryptionKey;
357     }
358
359     /**
360      * Creates a new MimeMessage using the given recipients and encryption.
361      */

362     public MimeMessage JavaDoc handleMessage(MimeMessage JavaDoc mm)
363       throws MessagingException JavaDoc, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
364       MimeMessage JavaDoc returnValue = new MimeMessage JavaDoc(mm);
365
366       /*
367         returnValue.setRecipients(Message.RecipientType.TO, getRecipients(Message.RecipientType.TO));
368         returnValue.setRecipients(Message.RecipientType.CC, getRecipients(Message.RecipientType.CC));
369         returnValue.setRecipients(Message.RecipientType.BCC, getRecipients(Message.RecipientType.BCC));
370       */

371
372       Key JavaDoc sigKey = getSignatureKey();
373       Key JavaDoc cryptoKey = getEncryptionKey();
374
375       if (sigKey instanceof EncryptionKey && cryptoKey instanceof EncryptionKey) {
376         if (((EncryptionKey)sigKey).getType() != ((EncryptionKey)cryptoKey).getType()) {
377           throw new MessagingException JavaDoc(Pooka.getProperty("error.NewMessage.differentEncryption", "Encryption and Signature Keys must be of same type (PGP or S/MIME)"));
378         }
379       }
380
381       if (getSignatureKey() != null) {
382         returnValue = Pooka.getCryptoManager().signMessage(returnValue, null, getSignatureKey());
383
384       }
385
386       if (getEncryptionKey() != null) {
387         returnValue = Pooka.getCryptoManager().encryptMessage(returnValue, getEncryptionKey());
388       }
389
390       return returnValue;
391     }
392   }
393
394 }
395
Popular Tags