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 ; 10 11 14 public class MessageCryptoInfo { 15 16 MessageInfo mMsgInfo; 18 19 String mEncryptionType = null; 21 22 boolean mCheckedEncryption = false; 24 25 boolean mCheckedSignature = false; 27 28 boolean mCheckedDecryption = false; 30 31 boolean mDecryptSuccessful = false; 33 34 boolean mSignatureValid = false; 36 37 40 public MessageCryptoInfo(MessageInfo sourceMsg) { 41 mMsgInfo = sourceMsg; 42 } 43 44 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 nspe) { 55 return null; 56 } 57 } else { 58 return null; 59 } 60 } 61 62 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 78 public String getEncryptionType() throws MessagingException { 79 checkEncryptionType(); 80 81 return mEncryptionType; 82 } 83 84 85 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 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 134 public boolean hasCheckedSignature() throws MessagingException { 135 if (! isSigned()) 136 return false; 137 138 return mCheckedSignature; 139 } 140 141 145 public boolean hasTriedDecryption() throws MessagingException { 146 if (! isEncrypted()) 147 return false; 148 149 return mCheckedDecryption; 150 } 151 152 156 public boolean isSignatureValid() throws MessagingException { 157 if (hasCheckedSignature()) 158 return mSignatureValid; 159 else 160 return false; 161 } 162 163 167 public boolean checkSignature(java.security.Key key, boolean recheck) throws MessagingException, java.io.IOException , java.security.GeneralSecurityException { 168 return checkSignature(key, recheck, true); 169 } 170 171 175 public boolean checkSignature(java.security.Key key, boolean recheck, boolean changeStatusOnFailure) throws MessagingException, java.io.IOException , java.security.GeneralSecurityException { 176 if (recheck || ! hasCheckedSignature()) { 177 EncryptionUtils cryptoUtils = getEncryptionUtils(); 178 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 197 public boolean decryptMessage(java.security.Key key, boolean recheck) 198 throws MessagingException, java.io.IOException , java.security.GeneralSecurityException { 199 return decryptMessage(key, recheck, true); 200 } 201 202 205 public boolean decryptMessage(java.security.Key key, boolean recheck, boolean changeStatusOnFailure) 206 throws MessagingException, java.io.IOException , java.security.GeneralSecurityException { 207 synchronized(this) { 208 if (mCheckedDecryption && ! recheck) { 209 return mDecryptSuccessful; 210 } else { 211 if (changeStatusOnFailure) 212 mCheckedDecryption = true; 213 214 AttachmentBundle bundle = mMsgInfo.getAttachmentBundle(); 216 List attachmentList = bundle.getAttachmentsAndTextPart(); 217 for (int i = 0; i < attachmentList.size(); i++) { 218 Object o = attachmentList.get(i); 219 if (o instanceof CryptoAttachment) { 220 CryptoAttachment ca = (CryptoAttachment) o; 221 222 if (! ca.decryptedSuccessfully()) { 223 EncryptionUtils cryptoUtils = getEncryptionUtils(); 225 226 BodyPart bp = ca.decryptAttachment(cryptoUtils, key); 227 228 232 233 242 MailUtilities.handlePart((MimeBodyPart) bp, bundle); 244 } 245 } 246 } 247 248 mDecryptSuccessful = true; 249 mCheckedDecryption = true; 250 } 251 } 252 253 return mDecryptSuccessful; 254 } 255 256 259 public boolean autoDecrypt(UserProfile defaultProfile) { 260 try { 261 String cryptType = getEncryptionType(); 262 263 java.security.Key [] 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 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 e) { 282 e.printStackTrace(); 283 } 284 } 285 286 } 287 } catch (Exception e) { 288 e.printStackTrace(); 289 } 290 return false; 291 } 292 293 297 public boolean autoCheckSignature(InternetAddress sender) { 298 try { 299 String senderAddress = sender.getAddress(); 300 Key [] 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 e) { 307 } 308 return false; 309 } 310 311 314 public Key [] extractKeys() throws MessagingException, java.io.IOException , java.security.GeneralSecurityException { 315 synchronized(this) { 316 AttachmentBundle bundle = mMsgInfo.getAttachmentBundle(); 317 List attachmentList = bundle.getAttachmentsAndTextPart(); 318 for (int i = 0; i < attachmentList.size(); i++) { 319 Object 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 333 public boolean isDecryptedSuccessfully() { 334 return mDecryptSuccessful; 335 } 336 337 340 public MessageInfo getMessageInfo() { 341 return mMsgInfo; 342 } 343 } 344 | Popular Tags |