KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > MessageCryptoInfo


1 package net.suberic.pooka;
2
3 import net.suberic.pooka.crypto.*;
4 import net.suberic.crypto.*;
5 import javax.mail.*;
6 import javax.mail.internet.*;
7
8 import java.util.*;
9 import java.security.Key JavaDoc;
10
11 /**
12  * This stores the encyrption information about a particular MessageInfo.
13  */

14 public class MessageCryptoInfo {
15
16   // the MessageInfo that we're analyzing.
17
MessageInfo mMsgInfo;
18
19   // the type of encryption (s/mime, pgp)
20
String JavaDoc mEncryptionType = null;
21
22   // whether or not we've checked to see if this is encrypted at all
23
boolean mCheckedEncryption = false;
24
25   // whether we've checked the signature yet
26
boolean mCheckedSignature = false;
27
28   // whether we've tried decrypting the message yet.
29
boolean mCheckedDecryption = false;
30
31   // whether or not the decryption was successful
32
boolean mDecryptSuccessful = false;
33
34   // whether the signature matches or not
35
boolean mSignatureValid = false;
36
37   /**
38    * Creates a MessageCryptoInfo for this given Message.
39    */

40   public MessageCryptoInfo(MessageInfo sourceMsg) {
41     mMsgInfo = sourceMsg;
42   }
43
44   /**
45    * Returns the EncryptionUtils to use with this MessageCryptoInfo.
46    */

47   public EncryptionUtils getEncryptionUtils() throws MessagingException {
48
49     checkEncryptionType();
50
51     if (mEncryptionType != null) {
52       try {
53         return net.suberic.crypto.EncryptionManager.getEncryptionUtils(mEncryptionType);
54       } catch (java.security.NoSuchProviderException JavaDoc nspe) {
55         return null;
56       }
57     } else {
58       return null;
59     }
60   }
61
62   /**
63    * Checks the encryption of this message.
64    */

65   void checkEncryptionType() throws MessagingException {
66     synchronized(this) {
67       if (! mCheckedEncryption) {
68         mEncryptionType = net.suberic.crypto.EncryptionManager.checkEncryptionType((MimeMessage) mMsgInfo.getMessage());
69         mCheckedEncryption = true;
70       }
71     }
72
73   }
74
75   /**
76    * Returns the encryption type of this message.
77    */

78   public String JavaDoc getEncryptionType() throws MessagingException {
79     checkEncryptionType();
80
81     return mEncryptionType;
82   }
83
84
85   /**
86    * Returns whether or not this message is signed.
87    */

88   public boolean isSigned() throws MessagingException {
89
90     if (mMsgInfo.hasLoadedAttachments()) {
91       List attachments = mMsgInfo.getAttachments();
92       for (int i = 0 ; i < attachments.size(); i++) {
93         if (attachments.get(i) instanceof SignedAttachment) {
94           return true;
95         }
96       }
97
98       return false;
99     } else {
100       EncryptionUtils utils = getEncryptionUtils();
101       if (utils != null) {
102         return (utils.getEncryptionStatus((MimeMessage) mMsgInfo.getMessage()) == EncryptionUtils.SIGNED);
103       } else
104         return false;
105     }
106   }
107
108   /**
109    * Returns whether or not this message is encrypted.
110    */

111   public boolean isEncrypted() throws MessagingException {
112
113     if (mMsgInfo.hasLoadedAttachments()) {
114       List attachments = mMsgInfo.getAttachments();
115       for (int i = 0 ; i < attachments.size(); i++) {
116         if (attachments.get(i) instanceof CryptoAttachment) {
117           return true;
118         }
119       }
120       return false;
121     } else {
122       EncryptionUtils utils = getEncryptionUtils();
123       if (utils != null) {
124         return (utils.getEncryptionStatus((MimeMessage) mMsgInfo.getMessage()) == EncryptionUtils.ENCRYPTED);
125       } else
126         return false;
127     }
128   }
129
130   /**
131    * Returns whether or not this message has had its signature checked.
132    * Returns false if the message is not signed in the first place.
133    */

134   public boolean hasCheckedSignature() throws MessagingException {
135     if (! isSigned())
136       return false;
137
138     return mCheckedSignature;
139   }
140
141   /**
142    * Returns whether or not this message has had a decryption attempt.
143    * Returns false if the message is not encrypted in the first place.
144    */

145   public boolean hasTriedDecryption() throws MessagingException {
146     if (! isEncrypted())
147       return false;
148
149     return mCheckedDecryption;
150   }
151
152   /**
153    * Returns whether or not the signature is valid. If the signature has not
154    * been checked yet, returns false.
155    */

156   public boolean isSignatureValid() throws MessagingException {
157     if (hasCheckedSignature())
158       return mSignatureValid;
159     else
160       return false;
161   }
162
163   /**
164    * Returns whether or not the signature is valid. If <code>recheck</code>
165    * is set to <code>true</code>, then checks again with the latest keys.
166    */

167   public boolean checkSignature(java.security.Key JavaDoc key, boolean recheck) throws MessagingException, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
168     return checkSignature(key, recheck, true);
169   }
170
171   /**
172    * Returns whether or not the signature is valid. If <code>recheck</code>
173    * is set to <code>true</code>, then checks again with the latest keys.
174    */

175   public boolean checkSignature(java.security.Key JavaDoc key, boolean recheck, boolean changeStatusOnFailure) throws MessagingException, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
176     if (recheck || ! hasCheckedSignature()) {
177       EncryptionUtils cryptoUtils = getEncryptionUtils();
178       //mSignatureValid = cryptoUtils.checkSignature((MimeMessage)mMsgInfo.getMessage(), key);
179
List attachments = mMsgInfo.getAttachments();
180       boolean returnValue = false;
181       for (int i = 0; i < attachments.size(); i++) {
182         Attachment current = (Attachment) attachments.get(i);
183         if (current instanceof SignedAttachment) {
184           mSignatureValid = ((SignedAttachment) current).checkSignature(cryptoUtils, key);
185         }
186       }
187       if (mSignatureValid || changeStatusOnFailure)
188         mCheckedSignature = true;
189     }
190
191     return mSignatureValid;
192   }
193
194   /**
195    * Tries to decrypt the message using the given Key.
196    */

197   public boolean decryptMessage(java.security.Key JavaDoc key, boolean recheck)
198     throws MessagingException, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
199     return decryptMessage(key, recheck, true);
200   }
201
202   /**
203    * Tries to decrypt the message using the given Key.
204    */

205   public boolean decryptMessage(java.security.Key JavaDoc key, boolean recheck, boolean changeStatusOnFailure)
206     throws MessagingException, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
207     synchronized(this) {
208       if (mCheckedDecryption && ! recheck) {
209         return mDecryptSuccessful;
210       } else {
211         if (changeStatusOnFailure)
212           mCheckedDecryption = true;
213
214         // run through all of the attachments and decrypt them.
215
AttachmentBundle bundle = mMsgInfo.getAttachmentBundle();
216         List attachmentList = bundle.getAttachmentsAndTextPart();
217         for (int i = 0; i < attachmentList.size(); i++) {
218           Object JavaDoc o = attachmentList.get(i);
219           if (o instanceof CryptoAttachment) {
220             CryptoAttachment ca = (CryptoAttachment) o;
221
222             if (! ca.decryptedSuccessfully()) {
223               // FIXME
224
EncryptionUtils cryptoUtils = getEncryptionUtils();
225
226               BodyPart bp = ca.decryptAttachment(cryptoUtils, key);
227
228               // check to see what kind of attachment it is. if it's a
229
// Multipart, then we need to expand it and add it to the
230
// attachment list.
231

232
233               /*
234                 if (bp.getContent() instanceof Multipart) {
235                 AttachmentBundle newBundle = MailUtilities.parseAttachments((Multipart) bp.getContent());
236                 bundle.addAll(newBundle);
237                 } else {
238                 bundle.removeAttachment(ca);
239                 bundle.addAttachment(ca);
240                 }
241               */

242               //bundle.removeAttachment(ca);
243
MailUtilities.handlePart((MimeBodyPart) bp, bundle);
244             }
245           }
246         }
247
248         mDecryptSuccessful = true;
249         mCheckedDecryption = true;
250       }
251     }
252
253     return mDecryptSuccessful;
254   }
255
256   /**
257    * Tries to decrypt the Message using all available cached keys.
258    */

259   public boolean autoDecrypt(UserProfile defaultProfile) {
260     try {
261       String JavaDoc cryptType = getEncryptionType();
262
263       // why not just try all of the private keys? at least, all the
264
// ones we have available.
265
//java.security.Key[] privateKeys = Pooka.getCryptoManager().getCachedPrivateKeys(cryptType);
266
java.security.Key JavaDoc[] privateKeys = Pooka.getCryptoManager().getCachedPrivateKeys();
267
268       if (privateKeys != null) {
269         for (int i = 0 ; i < privateKeys.length; i++) {
270           try {
271             if (privateKeys[i] instanceof EncryptionKey) {
272               // only try if the encryption type matches.
273
if (((EncryptionKey)privateKeys[i]).getEncryptionUtils().getType().equals(cryptType)) {
274                 if (decryptMessage(privateKeys[i], true, false))
275                   return true;
276               }
277             } else {
278               if (decryptMessage(privateKeys[i], true, false))
279                 return true;
280             }
281           } catch (Exception JavaDoc e) {
282             e.printStackTrace();
283           }
284         }
285
286       }
287     } catch (Exception JavaDoc e) {
288       e.printStackTrace();
289     }
290     return false;
291   }
292
293   /**
294    * Checks the signature of the given message as compared to the
295    * given from address.
296    */

297   public boolean autoCheckSignature(InternetAddress sender) {
298     try {
299       String JavaDoc senderAddress = sender.getAddress();
300       Key JavaDoc[] matchingKeys = Pooka.getCryptoManager().getPublicKeys(senderAddress,getEncryptionType());
301       for (int i = 0 ; i < matchingKeys.length; i++) {
302         if (checkSignature(matchingKeys[i], true, true)) {
303           return true;
304         }
305       }
306     } catch (Exception JavaDoc e) {
307     }
308     return false;
309   }
310
311   /**
312    * Extracts the (public) keys from the message.
313    */

314   public Key JavaDoc[] extractKeys() throws MessagingException, java.io.IOException JavaDoc, java.security.GeneralSecurityException JavaDoc {
315     synchronized(this) {
316       AttachmentBundle bundle = mMsgInfo.getAttachmentBundle();
317       List attachmentList = bundle.getAttachmentsAndTextPart();
318       for (int i = 0; i < attachmentList.size(); i++) {
319         Object JavaDoc o = attachmentList.get(i);
320         if (o instanceof KeyAttachment) {
321           EncryptionUtils utils = getEncryptionUtils();
322           return ((KeyAttachment) o).extractKeys(utils);
323         }
324       }
325     }
326
327     return null;
328   }
329
330   /**
331    * Returns true if this has been decrypted successfully.
332    */

333   public boolean isDecryptedSuccessfully() {
334     return mDecryptSuccessful;
335   }
336
337   /**
338    * Returns the MessageInfo.
339    */

340   public MessageInfo getMessageInfo() {
341     return mMsgInfo;
342   }
343 }
344
Popular Tags