- Timestamp:
- 05/10/19 14:48:21 (5 years ago)
- Location:
- trunk/src/org/expeditee/auth
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/auth/Actions.java
r1357 r1362 14 14 import java.security.PrivateKey; 15 15 import java.security.PublicKey; 16 import java.security.SecureRandom; 16 17 import java.security.cert.CertificateException; 17 18 import java.security.spec.InvalidKeySpecException; … … 26 27 import java.util.Map; 27 28 import java.util.Optional; 29 import java.util.Random; 28 30 import java.util.Scanner; 29 31 import java.util.stream.Collectors; … … 32 34 import javax.crypto.IllegalBlockSizeException; 33 35 import javax.crypto.NoSuchPaddingException; 36 import javax.crypto.SecretKey; 37 import javax.crypto.spec.SecretKeySpec; 34 38 35 39 import org.expeditee.agents.ExistingFramesetException; … … 44 48 import org.expeditee.auth.tags.AuthenticationTag; 45 49 import org.expeditee.core.Colour; 50 import org.expeditee.gio.gesture.StandardGestureActions; 46 51 import org.expeditee.gui.DisplayController; 47 52 import org.expeditee.gui.Frame; … … 81 86 Mail.sendMail(mail, recipient); 82 87 MessageBay.displayMessage("Test message sent."); 88 } 89 public static void SendTestOneOffMessage(String colleagueName) { 90 String time = Formatter.getDateTime(); 91 String sender = UserSettings.UserName.get(); 92 String topic = "Test Message"; 93 String message = "This is a test message."; 94 Map<String, String> options = new HashMap<String, String>(); 95 options.put("Neat", "Beep"); 96 MailEntry mail = new MailEntry(time, sender, colleagueName, topic, message, options); 97 Random rand = new SecureRandom(); 98 byte[] key = new byte[16]; 99 rand.nextBytes(key); 100 System.out.println(Base64.getEncoder().encodeToString(key)); 101 Mail.sendOneOffMail(mail, colleagueName, key); 102 } 103 public static void AuthOneOffSecureMessage(Text cursor, Text actionItem) { 104 byte[] keyBytes = Base64.getDecoder().decode(cursor.getText()); 105 SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 106 List<String> data = actionItem.getData(); 107 String topic = new String(org.expeditee.auth.sharing.Actions.DecryptSymmetric(Base64.getDecoder().decode(data.get(3)), key)); 108 String message = new String(org.expeditee.auth.sharing.Actions.DecryptSymmetric(Base64.getDecoder().decode(data.get(4)), key)); 109 Map<String, String> options = new HashMap<String, String>(); 110 for (int i = 5; i < data.size(); i+=2) { 111 String k = new String(org.expeditee.auth.sharing.Actions.DecryptSymmetric(Base64.getDecoder().decode(data.get(i)), key)); 112 String v = new String(org.expeditee.auth.sharing.Actions.DecryptSymmetric(Base64.getDecoder().decode(data.get(i + 1)), key)); 113 options.put(k, v); 114 } 115 MailBay.addMessage(data.get(0), topic, message, options); 116 StandardGestureActions.Refresh(); 83 117 } 84 118 public static void SetPWColleagues(String colleagueOne, String colleagueTwo) { -
trunk/src/org/expeditee/auth/AuthenticatorBrowser.java
r1357 r1362 219 219 } 220 220 221 Mail.addEntry(new Mail.MailEntry(timestamp, from, to, msg, msg2, options)); 221 Mail.MailEntry mail = new Mail.MailEntry(timestamp, from, to, msg, msg2, options); 222 mail.deadDropSource = dbFile; 223 Mail.addEntry(mail); 222 224 } 223 225 -
trunk/src/org/expeditee/auth/Mail.java
r1358 r1362 216 216 * If the private key can decrypt a message, then it was encrypted using the users public key, and is therefore for them. 217 217 */ 218 p ublicstatic List<MailEntry> getEntries(String name, PrivateKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {218 private static List<MailEntry> getEntries(String name, PrivateKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException { 219 219 List<MailEntry> filtered = new ArrayList<MailEntry>(); 220 220 221 221 for (MailEntry mail: messages) { 222 // confirm this is a message for the requester of entries 223 String receiver = mail.receiver; 224 byte[] receiverBytes = Base64.getDecoder().decode(receiver); 225 String receiverDecrypted = null; 226 Cipher c = Cipher.getInstance(AsymmetricAlgorithm + AsymmetricAlgorithmParameters); 227 try { 222 if (isEncryptedOneOffMessage(mail)) { 223 getOneOffSecureMail(name, filtered, mail); 224 } else { 225 getStandardSecureMail(name, key, filtered, mail); 226 } 227 } 228 229 return filtered; 230 } 231 232 private static void getOneOffSecureMail(String name, List<MailEntry> filtered, MailEntry mail) { 233 StringBuilder sb = new StringBuilder(); 234 String sep = ":::"; 235 sb.append("Read one-off secure message." + sep); 236 sb.append(mail.timestamp + sep); 237 sb.append(mail.sender + sep); 238 sb.append(mail.receiver + sep); 239 sb.append(mail.message + sep); 240 sb.append(mail.message2 + sep); 241 for (String k: mail.options.keySet()) { 242 sb.append(k + sep + mail.options.get(k) + sep); 243 } 244 sb.reverse().delete(0, 3).reverse(); 245 Map<String, String> options = new HashMap<String, String>(); 246 options.put(sb.toString(), "AuthOneOffSecureMessage"); 247 String currentTime = org.expeditee.stats.Formatter.getDateTime(); 248 MailEntry mailOuter = new MailEntry(currentTime, "Unknown", name, "You have received a one-off secure message.", "Check your email for more details.", options); 249 250 if (mail.deadDropSource != null) { 251 Path lastAccessedFile = mail.deadDropSource.getParent().resolve(name + ".last-accessed"); 252 SimpleDateFormat format = new SimpleDateFormat("ddMMMyyyy[HH:mm]"); 253 try (Scanner in = new Scanner(lastAccessedFile.toFile())) { 254 Date lastAccessedTimestamp = format.parse(in.nextLine()); 255 Date mailTimestamp = format.parse(mail.timestamp); 256 if (mailTimestamp.after(lastAccessedTimestamp)) { 257 filtered.add(mailOuter); 258 } 259 } catch (FileNotFoundException e) { 260 // It may not have been created yet, then err on the safe side and add it in. 261 filtered.add(mailOuter); 262 } catch (ParseException e) { 263 // If we fail to parse, then err on the safe side and add it in. 264 filtered.add(mailOuter); 265 } 266 } 267 } 268 269 private static void getStandardSecureMail(String name, PrivateKey key, List<MailEntry> filtered, MailEntry mail) 270 throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, InvalidKeyException, 271 BadPaddingException { 272 // confirm this is a message for the requester of entries 273 String receiver = mail.receiver; 274 byte[] receiverBytes = Base64.getDecoder().decode(receiver); 275 String receiverDecrypted = null; 276 Cipher c = Cipher.getInstance(AsymmetricAlgorithm + AsymmetricAlgorithmParameters); 277 try { 278 c.init(Cipher.DECRYPT_MODE, key); 279 receiverDecrypted = new String(c.doFinal(receiverBytes)); 280 } catch (InvalidKeyException | BadPaddingException e) { 281 return; 282 } 283 284 // add an unencrypted version of the message to the return list 285 if (receiverDecrypted.compareToIgnoreCase(name) == 0) { 286 c.init(Cipher.DECRYPT_MODE, key); 287 String sender = new String(c.doFinal(Base64.getDecoder().decode(mail.sender))); 288 c.init(Cipher.DECRYPT_MODE, key); 289 String message = new String(c.doFinal(Base64.getDecoder().decode(mail.message))); 290 c.init(Cipher.DECRYPT_MODE, key); 291 String message2 = new String(c.doFinal(Base64.getDecoder().decode(mail.message2))); 292 293 Map<String, String> options = new HashMap<String, String>(); 294 for (String label: mail.options.keySet()) { 228 295 c.init(Cipher.DECRYPT_MODE, key); 229 receiverDecrypted = new String(c.doFinal(receiverBytes)); 230 } catch (InvalidKeyException | BadPaddingException e) { 231 // this is not a message for 'us' 232 continue; 233 } 234 235 // add an unencrypted version of the message to the return list 236 if (receiverDecrypted.compareToIgnoreCase(name) == 0) { 296 String labelDecrypted = new String(c.doFinal(Base64.getDecoder().decode(label))); 237 297 c.init(Cipher.DECRYPT_MODE, key); 238 String sender = new String(c.doFinal(Base64.getDecoder().decode(mail.sender))); 239 c.init(Cipher.DECRYPT_MODE, key); 240 String message = new String(c.doFinal(Base64.getDecoder().decode(mail.message))); 241 c.init(Cipher.DECRYPT_MODE, key); 242 String message2 = new String(c.doFinal(Base64.getDecoder().decode(mail.message2))); 243 244 Map<String, String> options = new HashMap<String, String>(); //mail.options; 245 for (String label: mail.options.keySet()) { 246 c.init(Cipher.DECRYPT_MODE, key); 247 String labelDecrypted = new String(c.doFinal(Base64.getDecoder().decode(label))); 248 c.init(Cipher.DECRYPT_MODE, key); 249 String actionNameDecrypted = new String(c.doFinal(Base64.getDecoder().decode(mail.options.get(label)))); 250 options.put(labelDecrypted, actionNameDecrypted); 298 String actionNameDecrypted = new String(c.doFinal(Base64.getDecoder().decode(mail.options.get(label)))); 299 options.put(labelDecrypted, actionNameDecrypted); 300 } 301 302 Path lastAccessedFile = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(UserSettings.UserName.get() + "+" + sender).resolve(name + ".last-accessed"); 303 if (!lastAccessedFile.toFile().exists()) { 304 lastAccessedFile = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(sender + "+" + UserSettings.UserName.get()).resolve(name + ".last-accessed"); 305 } 306 SimpleDateFormat format = new SimpleDateFormat("ddMMMyyyy[HH:mm]"); 307 MailEntry mailEntry = new MailEntry(mail.timestamp, sender, receiverDecrypted, message, message2, options); 308 try (Scanner in = new Scanner(lastAccessedFile.toFile())) { 309 Date lastAccessedTimestamp = format.parse(in.nextLine()); 310 Date mailTimestamp = format.parse(mail.timestamp); 311 if (mailTimestamp.after(lastAccessedTimestamp)) { 312 filtered.add(mailEntry); 251 313 } 252 253 Path lastAccessedFile = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(UserSettings.UserName.get() + "+" + sender).resolve(name + ".last-accessed"); 254 if (!lastAccessedFile.toFile().exists()) { 255 lastAccessedFile = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(sender + "+" + UserSettings.UserName.get()).resolve(name + ".last-accessed"); 256 } 257 SimpleDateFormat format = new SimpleDateFormat("ddMMMyyyy[HH:mm]"); 258 MailEntry mailEntry = new MailEntry(mail.timestamp, sender, receiverDecrypted, message, message2, options); 259 try (Scanner in = new Scanner(lastAccessedFile.toFile())) { 260 Date lastAccessedTimestamp = format.parse(in.nextLine()); 261 Date mailTimestamp = format.parse(mail.timestamp); 262 if (mailTimestamp.after(lastAccessedTimestamp)) { 263 filtered.add(mailEntry); 264 } 265 } catch (FileNotFoundException e) { 266 // It may not have been created yet, then err on the safe side and add it in. 267 filtered.add(mailEntry); 268 } catch (ParseException e) { 269 // If we fail to parse, then err on the safe side and add it in. 270 filtered.add(mailEntry); 271 } 272 273 } 274 } 275 276 return filtered; 314 } catch (FileNotFoundException e) { 315 // It may not have been created yet, then err on the safe side and add it in. 316 filtered.add(mailEntry); 317 } catch (ParseException e) { 318 // If we fail to parse, then err on the safe side and add it in. 319 filtered.add(mailEntry); 320 } 321 322 } 323 } 324 325 private static boolean isEncryptedOneOffMessage(MailEntry mail) { 326 return mail.sender.charAt(0) == '='; 277 327 } 278 328 … … 281 331 */ 282 332 public static class MailEntry { 283 public String timestamp; ;333 public String timestamp; 284 334 public String sender; 285 335 public String receiver; … … 287 337 public String message2; 288 338 public Map<String, String> options; 289 public MailEntry subEntry;339 public Path deadDropSource; 290 340 291 341 public MailEntry(String timestamp, String sender, String rec, String message, String message2, Map<String, String> options) { -
trunk/src/org/expeditee/auth/gui/MailBay.java
r1313 r1362 152 152 } 153 153 154 155 154 public static void clear() { 156 155 getPreviewMessages().clear(); … … 298 297 int i = items.size(); 299 298 for (String content: options.keySet()) { 299 String[] split = content.split(":::"); 300 300 String action = options.get(content); 301 Text t = new Text(content); 301 Text t = new Text(split[0]); 302 for (int o = 1; o < split.length; o++) { 303 t.addToData(split[o]); 304 } 302 305 int y = MESSAGE_OFFSET_Y + (_previewMessages.size() + i) * SPACING; 303 306 t.setPosition(OFFSET_X, y);
Note:
See TracChangeset
for help on using the changeset viewer.