1 package net.suberic.pooka.gui; 2 3 import java.security.Key ; 4 import java.util.*; 5 6 import javax.mail.Address ; 7 import javax.mail.Message ; 8 import javax.mail.MessagingException ; 9 import javax.mail.internet.MimeMessage ; 10 import javax.mail.internet.InternetHeaders ; 11 import javax.mail.internet.InternetAddress ; 12 13 import net.suberic.pooka.*; 14 import net.suberic.crypto.*; 15 16 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 int mEncryptMessage = CRYPTO_DEFAULT; 31 32 int mSignMessage = CRYPTO_DEFAULT; 34 35 List mRecipientInfos = new LinkedList(); 37 38 41 public NewMessageCryptoInfo(NewMessageInfo nmi) { 42 super(nmi); 43 } 44 45 47 Key mSignatureKey = null; 49 50 Key mEncryptionKey = null; 52 53 56 public Key getSignatureKey() { 57 return mSignatureKey; 58 } 59 60 63 public void setSignatureKey(Key pSignatureKey) { 64 mSignatureKey = pSignatureKey; 65 } 66 67 70 public void setEncryptionKey(Key pEncryptionKey) { 71 mEncryptionKey = pEncryptionKey; 72 } 73 74 77 public Key getEncryptionKey() { 78 return mEncryptionKey; 79 } 80 81 83 86 public int getSignMessage() { 87 return mSignMessage; 88 } 89 90 93 public void setSignMessage(int pSignMessage) { 94 mSignMessage = pSignMessage; 95 } 96 97 99 102 public int getEncryptMessage() { 103 return mEncryptMessage; 104 } 105 106 109 public void setEncryptMessage(int pEncryptMessage) { 110 mEncryptMessage = pEncryptMessage; 111 } 112 113 115 118 public synchronized void attachEncryptionKey(Key key) { 119 if (! mAttachKeys.contains(key)) 120 mAttachKeys.add(key); 121 } 122 123 126 public synchronized void removeEncryptionKey(Key key) { 127 if (mAttachKeys.contains(key)) { 128 mAttachKeys.remove(key); 129 } 130 131 } 132 133 136 public List getAttachKeys() { 137 return new LinkedList(mAttachKeys); 138 } 139 140 142 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 [] { currentKey })); 154 } catch (Exception e) { 155 System.out.println("caught exception adding key to message: " + e); 157 e.printStackTrace(); 158 } 159 } 160 } 161 162 return keyParts; 163 } 164 165 168 public Map createEncryptedMessages(MimeMessage mm) throws MessagingException , java.io.IOException , java.security.GeneralSecurityException { 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 182 public List getCryptoRecipientInfos() { 183 return mRecipientInfos; 184 } 185 186 190 public boolean updateRecipientInfos(UserProfile profile, InternetHeaders headers) throws javax.mail.internet.AddressException , javax.mail.MessagingException { 191 193 String toHeader = headers.getHeader("To", ","); 194 if (toHeader == null) { 195 throw new MessagingException (Pooka.getProperty("error.NewMessage.noTo", "No To: recipient")); 196 } 197 InternetAddress [] toAddresses = InternetAddress.parse(headers.getHeader("To", ","), false); 198 if (toAddresses == null || toAddresses.length == 0) { 199 throw new MessagingException (Pooka.getProperty("error.NewMessage.noTo", "No To: recipient")); 200 } 201 202 String ccHeaderLine = headers.getHeader("CC", ","); 203 InternetAddress [] ccAddresses; 204 if (ccHeaderLine != null && ccHeaderLine.length() > 0) { 205 ccAddresses = InternetAddress.parse(ccHeaderLine, false); 206 } else { 207 ccAddresses = new InternetAddress [0]; 208 } 209 210 String bccHeaderLine = headers.getHeader("BCC", ","); 211 InternetAddress [] bccAddresses; 212 if (bccHeaderLine != null && bccHeaderLine.length() > 0) { 213 bccAddresses = InternetAddress.parse(bccHeaderLine, false); 214 } else { 215 bccAddresses = new InternetAddress [0]; 216 } 217 218 Key cryptKey = null; 219 220 if (getEncryptMessage() != CRYPTO_NO) 221 cryptKey = getEncryptionKey(); 222 223 Key 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 237 242 public class CryptoRecipientInfo { 243 244 Key mSignatureKey = null; 246 247 Key mEncryptionKey = null; 249 250 Address [] toList = null; 252 Address [] ccList = null; 253 Address [] bccList = null; 254 255 258 public CryptoRecipientInfo() { 259 260 } 261 262 266 public CryptoRecipientInfo(Key pSignatureKey, Key pEncryptionKey, Address [] pToList, Address [] pCcList, Address [] 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 280 public Address [] getRecipients(Message.RecipientType 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 294 public Address [] getAllRecipients() { 295 Address [] returnValue = new Address [0]; 296 returnValue = appendToArray(returnValue, toList); 297 returnValue = appendToArray(returnValue, ccList); 298 returnValue = appendToArray(returnValue, bccList); 299 300 return returnValue; 301 } 302 303 306 private Address [] appendToArray(Address [] original, Address [] toAdd) { 307 if (toAdd != null && toAdd.length > 0) { 308 int oldSize = original.length; 309 Address [] newReturnValue = new Address [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 321 public void setRecipients(Address [] pRecipients, Message.RecipientType 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 334 public Key getSignatureKey() { 335 return mSignatureKey; 336 } 337 338 341 public void setSignatureKey(Key pSignatureKey) { 342 mSignatureKey = pSignatureKey; 343 } 344 345 348 public void setEncryptionKey(Key pEncryptionKey) { 349 mEncryptionKey = pEncryptionKey; 350 } 351 352 355 public Key getEncryptionKey() { 356 return mEncryptionKey; 357 } 358 359 362 public MimeMessage handleMessage(MimeMessage mm) 363 throws MessagingException , java.io.IOException , java.security.GeneralSecurityException { 364 MimeMessage returnValue = new MimeMessage (mm); 365 366 371 372 Key sigKey = getSignatureKey(); 373 Key cryptoKey = getEncryptionKey(); 374 375 if (sigKey instanceof EncryptionKey && cryptoKey instanceof EncryptionKey) { 376 if (((EncryptionKey)sigKey).getType() != ((EncryptionKey)cryptoKey).getType()) { 377 throw new MessagingException (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 |