Ignore:
Timestamp:
01/30/19 12:49:25 (5 years ago)
Author:
bln4
Message:

Initial commit of functionality concerning multiuser login, further to come.

Actions.java -> Actions that allow users to authenticate and secure their accounts.
AuthenticationTag.java -> Enum like structure for text fields associated with authentication.
Authenticator.java -> Startup functionality for when Expeditee is run in authentication mode.
EncryptedExpReader.java -> Reads exp files previously encrypted with EncryptedExpWriter (not currently used) and EncryptedProfileExpWriter
Mail.java -> Functions for transforming database stored messages into datastructures used to display those messages to the MailBay.

File:
1 copied

Legend:

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

    r1201 r1202  
    1 package org.expeditee.auth.io;
     1package org.expeditee.auth;
    22
    33import java.io.BufferedReader;
    44import java.io.FileInputStream;
     5import java.io.FileReader;
    56import java.io.IOException;
    67import java.io.InputStreamReader;
     
    1920import javax.crypto.NoSuchPaddingException;
    2021import javax.crypto.SecretKey;
    21 
    22 import org.expeditee.auth.Authenticator;
     22import javax.crypto.spec.SecretKeySpec;
     23
    2324import org.expeditee.gui.Frame;
    2425import org.expeditee.io.ExpReader;
     26import org.expeditee.items.Text;
     27import org.expeditee.settings.auth.secrets.KeyList;
    2528import org.ngikm.Util.ThrowingFunction;
    2629import org.ngikm.cryptography.CryptographyConstants;
     
    2831public class EncryptedExpReader extends ExpReader implements CryptographyConstants {
    2932        static final String ENCRYPTED_EXP_FLAG = "EncryptedExp";
    30         private List<SecretKey> keys;
    31        
    32         public EncryptedExpReader(final String frameName) throws UnsupportedEncodingException {
     33        private SecretKey personalKey;
     34        private List<SecretKey> multiKey;
     35        private boolean accessDenied = false;
     36        private boolean usePersonalKey;
     37       
     38        public EncryptedExpReader(final String frameName, final boolean usePersonalKey) throws UnsupportedEncodingException {
    3339                super(frameName);
     40                this.usePersonalKey = usePersonalKey;
    3441        }
    3542       
     
    4148        }
    4249       
     50        public int getVersionEnc(String fullpath) {
     51                try {
     52                        BufferedReader reader;
     53                        if (usePersonalKey) {
     54                                reader = new EncryptedProfileLineReader(new BufferedReader(new FileReader(fullpath)));
     55                        } else {
     56                                reader = new EncryptedLineReader(new BufferedReader(new FileReader(fullpath)));
     57                        }
     58                        String next = "";
     59                        // First read the header lines until we get the version number
     60                        while (reader.ready() && !(next = reader.readLine()).equals("Z")) {
     61                                if (isValidLine(next)) {
     62                                        Character tag = getTag(next);
     63                                        String value = getValue(next);
     64                                        if (tag.equals('V')) {
     65                                                reader.close();
     66                                                return Integer.parseInt(value);
     67                                        }
     68                                }
     69                        }
     70                        reader.close();
     71                } catch (Exception e) {
     72                }
     73                return -1;
     74        }
     75       
    4376        @Override
    4477        public Frame readFrame(final String fullPath) throws IOException {
    4578                final Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8");
    46                 return readFrame(new EncryptedLineReader(new BufferedReader(in)));
    47         }
    48        
    49         public class EncryptedLineReader extends BufferedReader {
    50 
    51                 public EncryptedLineReader(final Reader in) {
    52                         super(in);
    53                 }
    54                                                                  
    55                 @Override
    56                 public String readLine() throws IOException {
    57                         final String line = super.readLine();   
    58                         if (line.isEmpty()) { return ""; }
    59                         if (line.startsWith(ENCRYPTED_EXP_FLAG)) {
    60                                 try {
    61                                         // resolve labels to secret keys
    62                                         final List<String> labels = Arrays.asList(line.split(" ")).stream().skip(1).collect(Collectors.toList());
    63                                         final Authenticator auth = new Authenticator();
    64                                         final ThrowingFunction<String, SecretKey, Exception> worker = new ThrowingFunction<String, SecretKey, Exception>() {
    65                                                 @Override
    66                                                 public SecretKey applyThrows(final String label) throws Exception {
    67                                                         return auth.getSecretKey(label, System.getProperty("password"));
    68                                                 }
    69                                         };
    70                                         keys = labels.stream().map(worker).collect(Collectors.toList());
    71                                         return readLine();
    72                                 } catch (final Exception e) {
    73                                         return "";
    74                                 }
    75                         }
    76                        
    77                         if (keys == null) {
    78                                 // We do not have permission to view this frame.
    79                                 return "";
    80                         }
    81                        
    82                         // use keys to decrypt
    83                         byte[] toDecrypt = Base64.getDecoder().decode(line);
    84                         for (final SecretKey key: keys) {
    85                                 toDecrypt = DecryptSymmetric(toDecrypt, key);
    86                         }
    87                         final String result = new String(toDecrypt);
    88                         if (result.startsWith("Z")) { return result.trim(); }
    89                         else { return result; }
    90                 }               
     79                if (usePersonalKey) {
     80                        return readFrame(new EncryptedProfileLineReader(new BufferedReader(in)));
     81                } else {
     82                        return readFrame(new EncryptedLineReader(new BufferedReader(in)));
     83                }
     84        }
     85       
     86        @Override
     87        public Frame readFrame(final BufferedReader reader) throws IOException {
     88                if (accessDenied) { return null; }
     89                else return super.readFrame(reader);
    9190        }
    9291       
     
    111110                }
    112111        }
     112       
     113        private class EncryptedProfileLineReader extends BufferedReader {
     114
     115                public EncryptedProfileLineReader(final Reader in) {
     116                        super(in);
     117                }
     118               
     119                @Override
     120                /**
     121                 * Reads a line from an encrypted exp file that uses profile encryption (single key; personal key)
     122                 * Returns that line to process, null if the currently logged in users personal key is not the appropriate one (access denied).
     123                 */
     124                public String readLine() throws IOException {
     125                        // read encrypted line
     126                        final String line = super.readLine();
     127                       
     128                        if (line.isEmpty()) { return ""; }
     129                        if (line.startsWith(ENCRYPTED_EXP_FLAG)) {
     130                                // record/overwrite previous personal key then ignore this line by recursing
     131                                final Text text = KeyList.PersonalKey.get();
     132                                final byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0));
     133                                personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     134                                return readLine();
     135                        }
     136                       
     137                        // decrypt line and return result
     138                        final byte[] toDecrypt = Base64.getDecoder().decode(line);
     139                        final byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey);
     140                        if (decrypted == null) {
     141                                accessDenied = true;
     142                                return null; // access denied
     143                        } else {
     144                                final String decryptedLine = new String(decrypted);
     145                                if (decryptedLine.startsWith("Z")) { return decryptedLine.trim(); }
     146                                else { return decryptedLine; }
     147                        }
     148                }       
     149        }
     150       
     151        private class EncryptedLineReader extends BufferedReader {
     152               
     153                public EncryptedLineReader(final Reader in) {
     154                        super(in);
     155                }
     156               
     157                @Override
     158                /**
     159                 * Reads a line from an encrypted exp file that uses (potentially multiple) labeled keys
     160                 * Returns that line to process, null if the ...
     161                 */
     162                public String readLine() throws IOException {
     163                        // read encrypted line
     164                        final String line = super.readLine();
     165                       
     166                        if (line.isEmpty()) { return ""; }
     167                        if (line.startsWith(ENCRYPTED_EXP_FLAG)) {
     168                                // resolve labels to secret keys
     169                                final List<String> labels = Arrays.asList(line.split(" ")).stream().skip(1).collect(Collectors.toList());
     170                                final ThrowingFunction<String, SecretKey, Exception> worker = new ThrowingFunction<String, SecretKey, Exception>() {
     171                                        @Override
     172                                        public SecretKey applyThrows(final String label) throws Exception {
     173                                                return Authenticator.getInstance().getSecretKey(label, System.getProperty("password"));
     174                                        }
     175                                };
     176                                multiKey = labels.stream().map(l -> {
     177                                        try {
     178                                                return worker.apply(l);
     179                                        } catch (final Exception e) {
     180                                                return null;
     181                                        }
     182                                }).collect(Collectors.toList());
     183                               
     184                                // confirm you have all the keys necessary for decryption
     185                                if (multiKey.contains(null)) {
     186                                        return null;
     187                                }
     188                               
     189                                // move onto the next line
     190                                return readLine();
     191                        }
     192                       
     193                        // decrypt line and return result
     194                        final byte[] toDecrypt = Base64.getDecoder().decode(line);
     195                        byte[] decryptedBytes = null;
     196                        for (final SecretKey key: multiKey) {
     197                                decryptedBytes = DecryptSymmetric(toDecrypt, key);
     198                                if (decryptedBytes == null) { return null; }
     199                        }
     200                        final String decrypted = new String(decryptedBytes);
     201                        if (decrypted.startsWith("Z")) { return decrypted.trim(); }
     202                        else { return decrypted; }
     203                }
     204        }
    113205}
Note: See TracChangeset for help on using the changeset viewer.