Changeset 1227 for trunk


Ignore:
Timestamp:
01/31/19 16:34:06 (5 years ago)
Author:
bln4
Message:

org.expeditee.auth.EncryptedExpReader ->
org.expeditee.auth.EncryptedExpWriter ->
Actions ->
AttributeUtils ->
Frame ->
FrameIO ->
UserSettings ->

Changed how reading and writing encrypted files worked. A Frame attribute is now consulted to determine what to use as key for encryption. The 'profile' attribute setting is used to signal that the users personal aes key is used. Further enhancement will mean that other labels will be able to be used.


Actions ->

MailMode action now consults the database to reaquire the mail.

Location:
trunk/src/org/expeditee
Files:
6 edited
1 moved

Legend:

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

    r1202 r1227  
    4242        /**
    4343         * Display Expeditee Mail
    44          */
    45         public static void MailMode() {
     44         * @throws IOException
     45         * @throws SQLException
     46         * @throws ClassNotFoundException
     47         * @throws CertificateException
     48         * @throws NoSuchAlgorithmException
     49         * @throws FileNotFoundException
     50         * @throws KeyStoreException
     51         */
     52        public static void MailMode() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, SQLException, IOException {
     53                if (!DisplayController.isMailMode()) {
     54                        Mail.clear();
     55                        Authenticator.getInstance().loadMailDatabase();
     56                }
    4657                DisplayController.ToggleMailMode();
    4758        }
     
    375386                                Frame frame = FrameIO.LoadFrame(profile.getFramesetName() + i);
    376387                                frame.setOwner(username);
     388                                frame.setEncryptionLabel("Profile");
    377389                                Collection<Item> items = frame.getAllItems();
    378390                                for (Item item: items) {
  • trunk/src/org/expeditee/auth/EncryptedExpReader.java

    r1202 r1227  
    1212import java.util.Arrays;
    1313import java.util.Base64;
    14 import java.util.List;
    15 import java.util.stream.Collectors;
    1614
    1715import javax.crypto.BadPaddingException;
     
    2624import org.expeditee.items.Text;
    2725import org.expeditee.settings.auth.secrets.KeyList;
    28 import org.ngikm.Util.ThrowingFunction;
    2926import org.ngikm.cryptography.CryptographyConstants;
     27
     28import sun.reflect.generics.reflectiveObjects.NotImplementedException;
    3029
    3130public class EncryptedExpReader extends ExpReader implements CryptographyConstants {
    3231        static final String ENCRYPTED_EXP_FLAG = "EncryptedExp";
    3332        private SecretKey personalKey;
    34         private List<SecretKey> multiKey;
     33        //private List<SecretKey> multiKey;
    3534        private boolean accessDenied = false;
    36         private boolean usePersonalKey;
    37        
    38         public EncryptedExpReader(final String frameName, final boolean usePersonalKey) throws UnsupportedEncodingException {
     35        //private boolean usePersonalKey;
     36
     37        public EncryptedExpReader(final String frameName) throws UnsupportedEncodingException {
    3938                super(frameName);
    40                 this.usePersonalKey = usePersonalKey;
    4139        }
    4240       
     
    5048        public int getVersionEnc(String fullpath) {
    5149                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                         }
     50                        BufferedReader reader = new EncryptedLineReader(new BufferedReader(new FileReader(fullpath)));
     51                        //if (usePersonalKey) {
     52                        //      reader = new EncryptedProfileLineReader(new BufferedReader(new FileReader(fullpath)));
     53                        //} else {
     54                        //      reader = new EncryptedLineReader(new BufferedReader(new FileReader(fullpath)));
     55                        //}
    5856                        String next = "";
    5957                        // First read the header lines until we get the version number
     
    7674        @Override
    7775        public Frame readFrame(final String fullPath) throws IOException {
    78                 final Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8");
    79                 if (usePersonalKey) {
    80                         return readFrame(new EncryptedProfileLineReader(new BufferedReader(in)));
    81                 } else {
    82                         return readFrame(new EncryptedLineReader(new BufferedReader(in)));
    83                 }
     76                Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8");
     77                return readFrame(new EncryptedLineReader(in));
     78                //if (usePersonalKey) {
     79                //      return readFrame(new EncryptedProfileLineReader(new BufferedReader(in)));
     80                //} else {
     81                //      return readFrame(new EncryptedLineReader(new BufferedReader(in)));
     82                //}
    8483        }
    8584       
     
    111110        }
    112111       
    113         private class EncryptedProfileLineReader extends BufferedReader {
    114 
    115                 public EncryptedProfileLineReader(final Reader in) {
     112        private class EncryptedLineReader extends BufferedReader {
     113                public EncryptedLineReader(Reader in) {
    116114                        super(in);
    117115                }
     
    119117                @Override
    120118                /**
    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).
     119                 * Reads a line from an encrypted exp file that uses an encryption specified by the first line of the file.
     120                 * Returns that line to process, null if the currently logged in users doesn't own the appropriate key (access denied).
    123121                 */
    124122                public String readLine() throws IOException {
    125                         // read encrypted line
    126                         final String line = super.readLine();
     123                        String line = super.readLine();
    127124                       
    128                         if (line.isEmpty()) { return ""; }
     125                        if (line.isEmpty()) {
     126                                return "";
     127                        }
    129128                        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();
     129                                String label = line.replace(ENCRYPTED_EXP_FLAG, "");
     130                                // if using Profile label, use personal key
     131                                if (label.startsWith("Profile")) {
     132                                        Text text = KeyList.PersonalKey.get();
     133                                        byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0));
     134                                        personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     135                                        return readLine();
     136                                } else {
     137                                        // TODO: resolve encryption label, what does this mean?
     138                                        personalKey = resolveLabel(label);
     139                                        return readLine();
     140                                }
    135141                        }
    136142                       
    137143                        // decrypt line and return result
    138                         final byte[] toDecrypt = Base64.getDecoder().decode(line);
    139                         final byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey);
     144                        byte[] toDecrypt = Base64.getDecoder().decode(line);
     145                        byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey);
    140146                        if (decrypted == null) {
    141147                                accessDenied = true;
    142148                                return null; // access denied
    143149                        } 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 {
     150                                String decryptedLine = new String(decrypted);
     151                                if (decryptedLine.startsWith("Z")) {
     152                                        return decryptedLine.trim();
     153                                } else {
     154                                        return decryptedLine;
     155                                }
     156                        }
     157                }
    152158               
    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         }
     159                private SecretKeySpec resolveLabel(String label) {
     160                        throw new NotImplementedException();
     161                }
     162        }
     163       
     164//      private class EncryptedProfileLineReader extends BufferedReader {
     165//
     166//              public EncryptedProfileLineReader(final Reader in) {
     167//                      super(in);
     168//              }
     169//             
     170//              @Override
     171//              /**
     172//               * Reads a line from an encrypted exp file that uses profile encryption (single key; personal key)
     173//               * Returns that line to process, null if the currently logged in users personal key is not the appropriate one (access denied).
     174//               */
     175//              public String readLine() throws IOException {
     176//                      // read encrypted line
     177//                      final String line = super.readLine();
     178//                     
     179//                      if (line.isEmpty()) { return ""; }
     180//                      if (line.startsWith(ENCRYPTED_EXP_FLAG)) {
     181//                              // record/overwrite previous personal key then ignore this line by recursing
     182//                              final Text text = KeyList.PersonalKey.get();
     183//                              final byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0));
     184//                              personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     185//                              return readLine();
     186//                      }
     187//                     
     188//                      // decrypt line and return result
     189//                      final byte[] toDecrypt = Base64.getDecoder().decode(line);
     190//                      final byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey);
     191//                      if (decrypted == null) {
     192//                              accessDenied = true;
     193//                              return null; // access denied
     194//                      } else {
     195//                              final String decryptedLine = new String(decrypted);
     196//                              if (decryptedLine.startsWith("Z")) { return decryptedLine.trim(); }
     197//                              else { return decryptedLine; }
     198//                      }
     199//              }       
     200//      }
     201//     
     202//      private class EncryptedLineReader extends BufferedReader {
     203//             
     204//              public EncryptedLineReader(final Reader in) {
     205//                      super(in);
     206//              }
     207//             
     208//              @Override
     209//              /**
     210//               * Reads a line from an encrypted exp file that uses (potentially multiple) labeled keys
     211//               * Returns that line to process, null if the ...
     212//               */
     213//              public String readLine() throws IOException {
     214//                      // read encrypted line
     215//                      final String line = super.readLine();
     216//                     
     217//                      if (line.isEmpty()) { return ""; }
     218//                      if (line.startsWith(ENCRYPTED_EXP_FLAG)) {
     219//                              // resolve labels to secret keys
     220//                              final List<String> labels = Arrays.asList(line.split(" ")).stream().skip(1).collect(Collectors.toList());
     221//                              final ThrowingFunction<String, SecretKey, Exception> worker = new ThrowingFunction<String, SecretKey, Exception>() {
     222//                                      @Override
     223//                                      public SecretKey applyThrows(final String label) throws Exception {
     224//                                              return Authenticator.getInstance().getSecretKey(label, System.getProperty("password"));
     225//                                      }
     226//                              };
     227//                              multiKey = labels.stream().map(l -> {
     228//                                      try {
     229//                                              return worker.apply(l);
     230//                                      } catch (final Exception e) {
     231//                                              return null;
     232//                                      }
     233//                              }).collect(Collectors.toList());
     234//                             
     235//                              // confirm you have all the keys necessary for decryption
     236//                              if (multiKey.contains(null)) {
     237//                                      return null;
     238//                              }
     239//                             
     240//                              // move onto the next line
     241//                              return readLine();
     242//                      }
     243//                     
     244//                      // decrypt line and return result
     245//                      final byte[] toDecrypt = Base64.getDecoder().decode(line);
     246//                      byte[] decryptedBytes = null;
     247//                      for (final SecretKey key: multiKey) {
     248//                              decryptedBytes = DecryptSymmetric(toDecrypt, key);
     249//                              if (decryptedBytes == null) { return null; }
     250//                      }
     251//                      final String decrypted = new String(decryptedBytes);
     252//                      if (decrypted.startsWith("Z")) { return decrypted.trim(); }
     253//                      else { return decrypted; }
     254//              }
     255//      }
    205256}
  • trunk/src/org/expeditee/auth/EncryptedExpWriter.java

    r1226 r1227  
    1919import org.ngikm.cryptography.CryptographyConstants;
    2020
    21 public class EncryptedProfileExpWriter extends ExpWriter implements CryptographyConstants {
    22         private SecretKey personalKey;
     21public class EncryptedExpWriter extends ExpWriter implements CryptographyConstants {
     22        private SecretKey key;
     23        private String label;
    2324        private static final String nl = "\n";
    2425       
    25         public EncryptedProfileExpWriter() throws IOException {
    26                 // obtain personal key
    27                 final Text text = KeyList.PersonalKey.get();
    28                 final byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0));
    29                 personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm);         
     26        public EncryptedExpWriter(String encryptionLabel) throws IOException {
     27                if (encryptionLabel.compareTo("Profile") == 0) {
     28                        // obtain personal key
     29                        Text text = KeyList.PersonalKey.get();
     30                        byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0));
     31                        key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     32                        label = "Profile";
     33                } else {
     34                        byte[] keyBytes = resolveKeyFromLabel(encryptionLabel);
     35                        key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     36                        label = encryptionLabel;
     37                }
    3038        }
    3139
     
    3341        protected void preOutputFrame() {
    3442                try {
    35                         final String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + nl;
     43                        String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + label + nl;
    3644                        _writer.write(line);
    3745                        _stringWriter.append(line);
     
    4755               
    4856                // prepare line to write out
    49                 final byte[] encrypted = EncryptSymmetric(line.getBytes(), personalKey);
    50                 final String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
     57                byte[] encrypted = EncryptSymmetric(line.getBytes(), key);
     58                String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
    5159               
    5260                // output
     
    5563        }
    5664       
    57         private static byte[] EncryptSymmetric(final byte[] toEncrypt, final SecretKey key) {
     65        private byte[] resolveKeyFromLabel(String label) {
     66                return null;
     67        }
     68       
     69        private static byte[] EncryptSymmetric(byte[] toEncrypt, SecretKey key) {
    5870                try {
    59                         final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
     71                        Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
    6072                        cipher.init(Cipher.ENCRYPT_MODE, key);
    6173                        //could use modulus
    62                         final int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
    63                         final byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
    64                         final byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
     74                        int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
     75                        byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
     76                        byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
    6577                        return result;
    6678                } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
  • trunk/src/org/expeditee/gui/AttributeUtils.java

    r1200 r1227  
    197197                        _FrameAttrib.put("BackgroundColor", Frame.class.getMethod("getBackgroundColor"),
    198198                                                            Frame.class.getMethod("setBackgroundColor", pColor));
     199                        _FrameAttrib.put("EncryptionLabel", Frame.class.getMethod("getEncryptionLabel"),
     200                                                                                                Frame.class.getMethod("setEncryptionLabel", pString));
     201                       
    199202                       
    200203                        // aliases for attribute setting
    201                         _FrameAttrib.alias("fgc",        "foregroundcolor");
    202                         _FrameAttrib.alias("bgc",        "backgroundcolor");
    203                         _FrameAttrib.alias("p",          "permission");
     204                        _FrameAttrib.alias("fgc",       "foregroundcolor");
     205                        _FrameAttrib.alias("bgc",       "backgroundcolor");
     206                        _FrameAttrib.alias("p",         "permission");
     207                        _FrameAttrib.alias("enc",               "encryptionlabel");
    204208                       
    205209                       
  • trunk/src/org/expeditee/gui/Frame.java

    r1217 r1227  
    163163        private Collection<FrameObserver> _observers = new HashSet<FrameObserver>();
    164164
     165        private String _encryptionLabel;
     166
    165167        /** Default constructor, nothing is set. */
    166168        public Frame()
     
    26222624        }
    26232625
    2624         public Collection<? extends Item> getInteractableItems()
    2625         {
     2626        public Collection<? extends Item> getInteractableItems() {
    26262627                /*
    26272628                 * TODO: Cache the interactableItems list so we dont have to recreate it
     
    26552656                return _interactableItems;
    26562657        }
     2658       
     2659        public String getEncryptionLabel() {
     2660                return _encryptionLabel;
     2661        }
     2662       
     2663        public void setEncryptionLabel(String label) {
     2664                _encryptionLabel = label;
     2665        }
    26572666
    26582667        private static final class History {
  • trunk/src/org/expeditee/gui/FrameIO.java

    r1219 r1227  
    4343import org.expeditee.auth.Authenticator;
    4444import org.expeditee.auth.EncryptedExpReader;
    45 import org.expeditee.auth.EncryptedProfileExpWriter;
     45import org.expeditee.auth.EncryptedExpWriter;
    4646import org.expeditee.auth.gui.MailBay;
    4747import org.expeditee.io.Conversion;
     
    9292                STATISTICS_DIR = PARENT_FOLDER + "statistics" + File.separator;
    9393                LOGS_DIR = PARENT_FOLDER + "logs" + File.separator;
     94                SHARED_BY_ME_FRAMESETS = PARENT_FOLDER + "framesets-shared-by-me" + File.separator;
     95                SHARED_WITH_ME_FRAMESETS = PARENT_FOLDER + "framesets-shared-with-me" + File.separator;
    9496        }
    9597
     
    125127
    126128        public static String STATISTICS_DIR;
     129       
     130        public static String SHARED_BY_ME_FRAMESETS;
     131       
     132        public static String SHARED_WITH_ME_FRAMESETS;
    127133
    128134        public static String LOGS_DIR;
     
    475481                        if (fullPath.endsWith(ExpReader.EXTENTION)) {
    476482                                if (EncryptedExpReader.isEncryptedExpediteeFile(fullPath)) {
    477                                         final boolean isProfile = frameName.startsWith(UserSettings.UserName.get());
    478                                         reader = new EncryptedExpReader(frameName, isProfile);
     483                                        //final boolean isProfile = frameName.startsWith(UserSettings.UserName.get());
     484                                        //reader = new EncryptedExpReader(frameName, isProfile);
     485                                        reader = new EncryptedExpReader(frameName);
    479486                                } else {
    480487                                        reader = new ExpReader(frameName);
     
    10061013                        // if its a new frame or an existing Exp frame...
    10071014                        if (fullPath == null || fullPath.endsWith(ExpReader.EXTENTION)) {
    1008                                 //Frame currentFrame = DisplayController.getCurrentFrame();
    10091015                                if (UserSettings.Authenticated.get() &&
    1010                                                 toSave.getName().startsWith(UserSettings.UserName.get()) &&
    1011                                                 toSave.getNumber() != Authenticator.PUBLIC_KEY_FRAME) {
    1012                                         writer = new EncryptedProfileExpWriter();
     1016                                                toSave.getNumber() != Authenticator.PUBLIC_KEY_FRAME &&
     1017                                                toSave.getEncryptionLabel() != null) {
     1018                                        writer = new EncryptedExpWriter(toSave.getEncryptionLabel());
    10131019                                        savedVersion = EncryptedExpReader.getVersion(fullPath);
    10141020                                } else {
  • trunk/src/org/expeditee/settings/UserSettings.java

    r1213 r1227  
    264264                FolderSettings.FrameDirs.get().add(FrameIO.HELP_PATH);
    265265                FolderSettings.FrameDirs.get().add(FrameIO.MESSAGES_PATH);
     266                FolderSettings.FrameDirs.get().add(FrameIO.SHARED_BY_ME_FRAMESETS);
     267                FolderSettings.FrameDirs.get().add(FrameIO.SHARED_WITH_ME_FRAMESETS);
    266268                FolderSettings.FrameDirs.setDefault(FolderSettings.FrameDirs.get());
    267269                FolderSettings.ImageDirs.get().add(FrameIO.IMAGES_PATH);
Note: See TracChangeset for help on using the changeset viewer.