Ignore:
Timestamp:
05/07/19 14:08:23 (5 years ago)
Author:
bln4
Message:

Fixes to permission on items.

File:
1 edited

Legend:

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

    r1320 r1357  
    3333import javax.crypto.IllegalBlockSizeException;
    3434import javax.crypto.NoSuchPaddingException;
     35import javax.crypto.SecretKey;
     36import javax.crypto.spec.SecretKeySpec;
    3537
    3638import org.expeditee.auth.gui.MailBay;
     
    5456        }
    5557       
     58        public static void sendOneOffMail(MailEntry mail, String colleagueName, byte[] key) {
     59                // Ensure dead drop area is set up.
     60                Path databaseFileDirPath = ensureDeadDrops(colleagueName);
     61               
     62                // Ensure the database file exists.
     63                Path databaseFilePath = ensureDatabaseFile(colleagueName, databaseFileDirPath);
     64               
     65                // Create secret key.
     66                SecretKey secretKey = new SecretKeySpec(key, SymmetricAlgorithm);
     67               
     68                // Send message
     69                sendMail(mail, secretKey, databaseFilePath);
     70        }
     71       
    5672        public static void sendMail(MailEntry mail, String colleagueName) {
    5773                // Ensure dead drop area is set up.
    58                 String me = UserSettings.UserName.get().toLowerCase();
    59                 String them = colleagueName.toLowerCase();
    60                 Path databaseFileDirPath = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(me + "+" + them);
    61                 if (!databaseFileDirPath.toFile().exists()) {
    62                         databaseFileDirPath = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(them + "+" + me);
    63                 }
     74                Path databaseFileDirPath = ensureDeadDrops(colleagueName);
     75               
     76                // Ensure the database file exists.
     77                Path databaseFilePath = ensureDatabaseFile(colleagueName, databaseFileDirPath);
     78               
     79                // Obtain public key
     80                PublicKey publicKey = null;
     81                try {
     82                        publicKey = AuthenticatorBrowser.getInstance().getPublicKey(colleagueName);
     83                } catch (InvalidKeySpecException | NoSuchAlgorithmException | KeyStoreException | CertificateException
     84                                | ClassNotFoundException | IOException | SQLException e) {
     85                        System.err.println("Error while sending message.  Unable to obtain public key for colleague " + 
     86                                colleagueName + ".  Exception message: " + e.getMessage());
     87                        return;
     88                }
     89               
     90                // Check we got public key
     91                if (publicKey == null) {
     92                        System.err.println("Error while sending message.  Unable to obtain public key for colleague.  Have you exchanged contact details?");
     93                        return;
     94                }
     95               
     96                // Send message
     97                sendMail(mail, publicKey, databaseFilePath);
     98        }
     99
     100        private static Path ensureDatabaseFile(String colleagueName, Path databaseFileDirPath) {
    64101                Path databaseFilePath = databaseFileDirPath.resolve(colleagueName + ".db");
    65102                File databaseFile = databaseFilePath.toFile();
     
    86123                        }
    87124                }
    88                
    89                 // Obtain public key
    90                 PublicKey publicKey = null;
    91                 try {
    92                         publicKey = AuthenticatorBrowser.getInstance().getPublicKey(colleagueName);
    93                 } catch (InvalidKeySpecException | NoSuchAlgorithmException | KeyStoreException | CertificateException
    94                                 | ClassNotFoundException | IOException | SQLException e) {
    95                         System.err.println("Error while sending message.  Unable to obtain public key for colleague " + 
    96                                 colleagueName + ".  Exception message: " + e.getMessage());
    97                         return;
    98                 }
    99                
    100                 // Check we got public key
    101                 if (publicKey == null) {
    102                         System.err.println("Error while sending message.  Unable to obtain public key for colleague.  Have you exchanged contact details?");
    103                         return;
    104                 }
    105                
    106                 // Send message
    107                 sendMail(mail, publicKey, databaseFilePath);
     125                return databaseFilePath;
     126        }
     127
     128        private static Path ensureDeadDrops(String colleagueName) {
     129                String me = UserSettings.UserName.get().toLowerCase();
     130                String them = colleagueName.toLowerCase();
     131                Path databaseFileDirPath = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(me + "+" + them);
     132                if (!databaseFileDirPath.toFile().exists()) {
     133                        databaseFileDirPath = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(them + "+" + me);
     134                }
     135                return databaseFileDirPath;
    108136        }
    109137       
     
    111139                try {
    112140                        Cipher cipher = Cipher.getInstance(AsymmetricAlgorithm + AsymmetricAlgorithmParameters);
    113                        
    114                         // encrypt the necessary parts of the message
    115                         //cipher.init(Cipher.ENCRYPT_MODE, key);
    116                         //String time = Base64.getEncoder().encodeToString(cipher.doFinal(mail.timestamp.getBytes()));
    117141                        cipher.init(Cipher.ENCRYPT_MODE, key);
    118142                        String sender = Base64.getEncoder().encodeToString(cipher.doFinal(mail.sender.getBytes()));
     
    134158                       
    135159                        // write to mail database
    136                         Connection c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile);
    137                         String sql = "INSERT INTO EXPMAIL (TIME,SND,REC,MSG,MSG2,OPTS,OPTSVAL) VALUES (?, ?, ?, ?, ?, ?, ?);";
    138                         PreparedStatement statement = c.prepareStatement(sql);
    139                         statement.setString(1, mail.timestamp);
    140                         statement.setString(2, sender);
    141                         statement.setString(3, rec);
    142                         statement.setString(4, message);
    143                         statement.setString(5, message2);
    144                         String opts = Arrays.toString(options.keySet().toArray());
    145                         statement.setString(6, opts);
    146                         String optsval = Arrays.toString(options.values().toArray());
    147                         statement.setString(7, optsval);
    148                         statement.execute();
    149                         statement.close();
    150                         c.close();                     
     160                        writeToMailDatabase(mail, databaseFile, sender, rec, message, message2, options);                       
    151161                } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | SQLException e) {
    152162                        e.printStackTrace();
     
    154164        }
    155165       
     166        private static void sendMail(MailEntry mail, SecretKey key, Path databaseFile) {
     167                try {
     168                        //      Encrypt message.
     169                        Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
     170                        cipher.init(Cipher.ENCRYPT_MODE, key);
     171                        String sender = "=" + Base64.getEncoder().encodeToString(cipher.doFinal(mail.sender.getBytes()));
     172                        cipher.init(Cipher.ENCRYPT_MODE, key);
     173                        String rec = Base64.getEncoder().encodeToString(cipher.doFinal(mail.receiver.getBytes()));
     174                        cipher.init(Cipher.ENCRYPT_MODE, key);
     175                        String message = Base64.getEncoder().encodeToString(cipher.doFinal(mail.message.getBytes()));
     176                        cipher.init(Cipher.ENCRYPT_MODE, key);
     177                        String message2 = Base64.getEncoder().encodeToString(cipher.doFinal(mail.message2.getBytes()));                 
     178                        Map<String, String> options = new HashMap<String, String>();
     179                        for (String label: mail.options.keySet()) {
     180                                cipher.init(Cipher.ENCRYPT_MODE, key);
     181                                String labelEncrypted = Base64.getEncoder().encodeToString(cipher.doFinal(label.getBytes()));
     182                                cipher.init(Cipher.ENCRYPT_MODE, key);
     183                                String actionNameEncrypted = Base64.getEncoder().encodeToString(cipher.doFinal(mail.options.get(label).getBytes()));
     184                                options.put(labelEncrypted, actionNameEncrypted);
     185                        }
     186                       
     187                        //      Write to mail database.
     188                        writeToMailDatabase(mail, databaseFile, sender, rec, message, message2, options);
     189                } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | SQLException e) {
     190                        e.printStackTrace();
     191                }
     192        }
     193
     194        private static void writeToMailDatabase(MailEntry mail, Path databaseFile, String sender, String rec,
     195                        String message, String message2, Map<String, String> options) throws SQLException {
     196                Connection c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile);
     197                String sql = "INSERT INTO EXPMAIL (TIME,SND,REC,MSG,MSG2,OPTS,OPTSVAL) VALUES (?, ?, ?, ?, ?, ?, ?);";
     198                PreparedStatement statement = c.prepareStatement(sql);
     199                statement.setString(1, mail.timestamp);
     200                statement.setString(2, sender);
     201                statement.setString(3, rec);
     202                statement.setString(4, message);
     203                statement.setString(5, message2);
     204                String opts = Arrays.toString(options.keySet().toArray());
     205                statement.setString(6, opts);
     206                String optsval = Arrays.toString(options.values().toArray());
     207                statement.setString(7, optsval);
     208                statement.execute();
     209                statement.close();
     210                c.close();
     211        }
     212               
    156213        /**
    157214         * Gets the mail messages that the specified user is able to read.
     
    230287                public String message2;
    231288                public Map<String, String> options;
     289                public MailEntry subEntry;
    232290               
    233291                public MailEntry(String timestamp, String sender, String rec, String message, String message2, Map<String, String> options) {
Note: See TracChangeset for help on using the changeset viewer.