Changeset 1362 for trunk


Ignore:
Timestamp:
05/10/19 14:48:21 (5 years ago)
Author:
bln4
Message:

Added ability to send one off secure messages.

Location:
trunk/src/org/expeditee/auth
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/auth/Actions.java

    r1357 r1362  
    1414import java.security.PrivateKey;
    1515import java.security.PublicKey;
     16import java.security.SecureRandom;
    1617import java.security.cert.CertificateException;
    1718import java.security.spec.InvalidKeySpecException;
     
    2627import java.util.Map;
    2728import java.util.Optional;
     29import java.util.Random;
    2830import java.util.Scanner;
    2931import java.util.stream.Collectors;
     
    3234import javax.crypto.IllegalBlockSizeException;
    3335import javax.crypto.NoSuchPaddingException;
     36import javax.crypto.SecretKey;
     37import javax.crypto.spec.SecretKeySpec;
    3438
    3539import org.expeditee.agents.ExistingFramesetException;
     
    4448import org.expeditee.auth.tags.AuthenticationTag;
    4549import org.expeditee.core.Colour;
     50import org.expeditee.gio.gesture.StandardGestureActions;
    4651import org.expeditee.gui.DisplayController;
    4752import org.expeditee.gui.Frame;
     
    8186                Mail.sendMail(mail, recipient);
    8287                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();
    83117        }
    84118        public static void SetPWColleagues(String colleagueOne, String colleagueTwo) {
  • trunk/src/org/expeditee/auth/AuthenticatorBrowser.java

    r1357 r1362  
    219219                        }
    220220                       
    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);
    222224                }
    223225               
  • trunk/src/org/expeditee/auth/Mail.java

    r1358 r1362  
    216216         * If the private key can decrypt a message, then it was encrypted using the users public key, and is therefore for them.
    217217         */
    218         public static 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 {
    219219                List<MailEntry> filtered = new ArrayList<MailEntry>();
    220220               
    221221                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()) {
    228295                                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)));
    237297                                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);
    251313                                }
    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) == '=';
    277327        }
    278328       
     
    281331         */
    282332        public static class MailEntry {
    283                 public String timestamp;;
     333                public String timestamp;
    284334                public String sender;
    285335                public String receiver;
     
    287337                public String message2;
    288338                public Map<String, String> options;
    289                 public MailEntry subEntry;
     339                public Path deadDropSource;
    290340               
    291341                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  
    152152        }
    153153       
    154 
    155154        public static void clear() {
    156155                getPreviewMessages().clear();
     
    298297                        int i = items.size();
    299298                        for (String content: options.keySet()) {
     299                                String[] split = content.split(":::");
    300300                                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                                }
    302305                                int y = MESSAGE_OFFSET_Y + (_previewMessages.size() + i) * SPACING;
    303306                                t.setPosition(OFFSET_X, y);
Note: See TracChangeset for help on using the changeset viewer.