Ignore:
Timestamp:
06/25/19 13:57:37 (5 years ago)
Author:
bln4
Message:

Changed surrogates to work the way discussed with David. EncryptedExpReader/Writer updated to work with this.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/encryption/io/EncryptedExpWriter.java

    r1410 r1413  
    66import java.security.InvalidKeyException;
    77import java.security.NoSuchAlgorithmException;
    8 import java.util.ArrayList;
    98import java.util.Arrays;
    109import java.util.Base64;
    1110import java.util.LinkedHashMap;
    1211import java.util.List;
    13 import java.util.Set;
    14 import java.util.function.Consumer;
     12import java.util.function.BinaryOperator;
     13import java.util.function.Function;
     14import java.util.stream.Collectors;
    1515
    1616import javax.crypto.BadPaddingException;
     
    2424import org.expeditee.encryption.items.surrogates.EncryptionDetail;
    2525import org.expeditee.encryption.items.surrogates.Label;
    26 import org.expeditee.encryption.items.surrogates.Surrogate;
    2726import org.expeditee.encryption.items.surrogates.Label.LabelResult;
    2827import org.expeditee.gui.Frame;
     
    7675       
    7776        @Override
    78         public void outputFrame(Frame frame) throws IOException {
    79                 if (_writer == null) { return; }
    80                
    81                 preOutputFrame();
    82                 writeHeader(frame);
    83                
    84                 // write item
    85                 writeItemData(frame);
    86                 writeTerminator();
    87                
    88                 // write lines and constraints
    89                 writeLineData();
    90                 writeTerminator();
    91                 writeConstraintData();
    92                 writeTerminator();
    93                
    94                 writeLine(SessionStats.getFrameEventList(frame));
    95         }
    96        
    97         @Override
    9877        protected void preOutputFrame() {
    9978                try {
     
    10786       
    10887        @Override
     88        public void outputFrame(Frame frame) throws IOException {
     89                if (_writer == null) { return; }
     90               
     91                preOutputFrame();
     92                writeHeader(frame);
     93               
     94                // write item
     95                writeItemData(frame);
     96                writeTerminator();
     97               
     98                // write lines and constraints
     99                writeLineData();
     100                writeTerminator();
     101                writeConstraintData();
     102                writeTerminator();
     103               
     104                writeLine(SessionStats.getFrameEventList(frame));
     105        }
     106       
     107        @Override
    109108        protected void writeLine(String line) throws IOException {
    110109                // do not write empty lines
     
    125124        }
    126125       
    127         @Override
    128         protected void writeLineData() throws IOException {
    129                 super.writeLineData();
    130                
    131                 writeSurrogateLineData();
    132         }
    133        
    134         @Override
    135126        protected void writeClass(Item toWrite) throws IOException {
    136127                LinkedHashMap<Character,Method> itemTags = new LinkedHashMap<Character, Method>(getItemTags());
    137128                LinkedHashMap<String,Method> itemTagsExt = new LinkedHashMap<String, Method>(getItemTagsExt());
    138129               
    139                 // Perform final update on surrogates to ensure inheritance works.
    140                 Set<Surrogate> surrogates = toWrite.getSurrogates();
    141                 for (Surrogate s: surrogates) {
    142                         s.toString();
    143                 }
    144                
    145130                writeTag(toWrite, new Object[] {}, itemTags, 'S');
    146131                writeTag(toWrite, new Object[] {}, itemTagsExt, "_el");
    147132               
     133                if (toWrite.isSurrogate()) {
     134                        writeLine("SurrogateFor " + toWrite.getClassic().getID());
     135                }
     136               
    148137                itemTags.remove('S');
    149138                itemTagsExt.remove("_el");
    150                
     139                               
    151140                writeTags(toWrite, new Object[] {}, itemTags);
    152141                writeTags(toWrite, new Object[] {}, itemTagsExt);
     
    155144        @Override
    156145        protected <T> void writeTag(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags, T tag) {
     146                if (toWrite.isSurrogate() && toWrite.isTagInherited(tag + "")) {
     147                        return;
     148                }
    157149                EncryptionDetail encryptionDetail = toWrite.getEncryptionDetailForTag(tag + "");
    158                 switch (encryptionDetail) {
     150                               
     151                switch(encryptionDetail) {
    159152                case UnencryptedOnSave:
    160                         super.writeTag(toWrite, param, tags, tag);
     153                        writeTagUnencryptedOnSave(toWrite, param, tags, tag);
     154                        break;
     155                case InheritanceCheckOnSave:
     156                        writeTagInheritanceCheckOnSave(toWrite, tags, tag);
    161157                        break;
    162158                case ReencryptOnSave:
    163                         Method toRun = tags.get(tag);
    164                         Class<?> declarer = toRun.getDeclaringClass();
    165                         LabelResult res = Label.resolveKey(toWrite.getEncryptionLabel());
    166                         if (declarer.isAssignableFrom(toWrite.getClass()) && res == LabelResult.SuccessResolveLabelToKey) {
    167                                 SecretKey key = new SecretKeySpec(res.key, SymmetricAlgorithm);
    168                                 try {
    169                                         Object o = toRun.invoke(toWrite, new Object[] {});
    170                                         o = Conversion.ConvertToExpeditee(toRun, o);
    171                                         if (o != null) {
    172                                                 if (o instanceof List) {
    173                                                         for (Object line: (List<?>)o) {
    174                                                                 byte[] encryptedBytes = EncryptSymmetric(line.toString().getBytes(), key);
    175                                                                 writeLine(tag + "E", Base64.getEncoder().encodeToString(encryptedBytes));
    176                                                         }
    177                                                 } else {
    178                                                         byte[] encryptedBytes = EncryptSymmetric(o.toString().getBytes(), key);
    179                                                         writeLine(tag + "E", Base64.getEncoder().encodeToString(encryptedBytes));
    180                                                 }
    181                                         }
    182                                 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
    183                                         e.printStackTrace();
    184                                 } catch (IOException e) {
    185                                         e.printStackTrace();
    186                                 }
    187                         }
     159                        writeTagReencryptOnSave(toWrite, tags, tag);
    188160                        break;
    189161                case UseUndecipheredValueOnSave:
    190                         try {
    191                                 writeLine(tag + "E", encryptionDetail.getUndecipheredValue());
    192                         } catch (IOException e) {
    193                                 e.printStackTrace();
    194                         }
     162                        writeTagUseUndecipheredValueOnSave(toWrite, tags, tag);
    195163                        break;
    196164                }
     
    204172                }
    205173               
    206                 for (final Item i: frame.getBodyItemsWithInsufficientPermissions()) {
     174                for (Item i: frame.getBodyItemsWithInsufficientPermissions()) {
    207175                        assert (!(i instanceof Line));
    208176                        writeItem(i);
    209177                }
    210178               
    211                 writeSurrogateItemsData(frame);
    212         }
    213        
    214         private void writeSurrogateItemsData(Frame frame) throws IOException {
    215                 List<Surrogate> surrogates = new ArrayList<Surrogate>();
    216                
    217                 Consumer<? super Item> store = item -> surrogates.addAll(item.getSurrogates());
    218                 frame.getItemsToSave().forEach(store);
    219                 frame.getBodyItemsWithInsufficientPermissions().forEach(store);
    220                
     179                List<Item> surrogates = frame.getSurrogateItemsToSave();
    221180                if (!surrogates.isEmpty()) {
    222                         // If there is some surrogates, write the surrogate
    223                         // terminator, then write out the surrogates.
    224181                        writeSurrogateTerminator();
    225                        
    226                         for (Surrogate surrogate: surrogates) {
    227                                 String output = surrogate.toString();
    228                                 writeLine(output);
    229                         }
    230                 }
    231         }
    232 
    233         private void writeSurrogateLineData() throws IOException {
    234                 List<Surrogate> surrogateLines = new ArrayList<Surrogate>();
    235                
    236                 for(Item lineEnd: _lineEnds) {
    237                         List<Line> lines = lineEnd.getLines();
    238                         for (Line line: lines) {
    239                                 surrogateLines.addAll(line.getSurrogates());
    240                         }
    241                 }
    242                
    243                 if (!surrogateLines.isEmpty()) {
    244                         // If there is some surrogates, write the surrogate
    245                         // terminator, then write out the surrogates.
    246                         writeSurrogateTerminator();
    247                        
    248                         for (Surrogate surrogate: surrogateLines) {
    249                                 String output = surrogate.toString();
    250                                 writeLine(output);
    251                         }
    252                 }
     182                        for (Item i: surrogates) {
     183                                assert (!(i instanceof Line));
     184                                writeItem(i);
     185                        }
     186                }
     187        }
     188       
     189        private <T> void writeTagInheritanceCheckOnSave(Item toWrite, LinkedHashMap<T, Method> tags, T tag) {   
     190                List<Item> surrogateItems = toWrite.getSurrogates().stream().collect(Collectors.toList());
     191                Function<Item, Boolean> isTagInherited = surrogate -> surrogate.isTagInherited(tag + "");
     192                BinaryOperator<Boolean> trueExists = (a, b) -> a || b;
     193                boolean surrogatesInherit = surrogateItems.stream().map(isTagInherited).collect(Collectors.reducing(trueExists)).get();
     194                if (surrogatesInherit &&
     195                                Label.resolveKey(toWrite.getEncryptionLabel()) == LabelResult.SuccessResolveLabelToKey) {
     196                        toWrite.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
     197                        writeTagUnencryptedOnSave(toWrite, new Object[] {}, tags, tag);
     198                } else {
     199                        toWrite.setEncryptionDetailForTag(tag + "", EncryptionDetail.ReencryptOnSave);
     200                        writeTagReencryptOnSave(toWrite, tags, tag);
     201                }
     202        }
     203       
     204        private <T> void writeTagReencryptOnSave(Item toWrite, LinkedHashMap<T, Method> tags, T tag) {
     205                Method toRun = tags.get(tag);
     206                Class<?> declarer = toRun.getDeclaringClass();
     207                LabelResult res = Label.resolveKey(toWrite.getEncryptionLabel());
     208                if (declarer.isAssignableFrom(toWrite.getClass()) && res == LabelResult.SuccessResolveLabelToKey) {
     209                        try {
     210                                Object o = toRun.invoke(toWrite, new Object[] {});
     211                                o = Conversion.ConvertToExpeditee(toRun, o);
     212                                if (o == null) {
     213                                        return;
     214                                }
     215                                if (o instanceof List) {
     216                                        for (Object line: (List<?>) o) {
     217                                                writeLineEnc(tag, line, res.key);
     218                                        }
     219                                } else {
     220                                        writeLineEnc(tag, o, res.key);
     221                                }
     222                        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
     223                                e.printStackTrace();
     224                        } catch (IOException e) {
     225                                e.printStackTrace();
     226                        }
     227                }
     228        }
     229       
     230        private <T> void writeTagUseUndecipheredValueOnSave(Item toWrite, LinkedHashMap<T, Method> tags, T tag) {
     231                EncryptionDetail encryptionDetail = toWrite.getEncryptionDetailForTag(tag + "");
     232                try {
     233                        writeLine(tag + "E", encryptionDetail.getUndecipheredValue());
     234                } catch (IOException e) {
     235                        e.printStackTrace();
     236                }
     237        }
     238       
     239        private <T> void writeTagUnencryptedOnSave(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags, T tag) {
     240                super.writeTag(toWrite, param, tags, tag);
    253241        }
    254242       
    255243        private void writeSurrogateTerminator() throws IOException {
    256244                writeLine(SURROGATE_TERMINATOR + nl);
     245        }
     246       
     247        private <T> void writeLineEnc(T tag, Object line, byte[] keyBytes) throws IOException {
     248                SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     249                byte[] lineEncryptedBytes = EncryptSymmetric(line.toString().getBytes(), key);
     250                line = Base64.getEncoder().encodeToString(lineEncryptedBytes);
     251                writeLine(tag + "E", line.toString());
    257252        }
    258253       
     
    271266                        return null;
    272267                }
    273         }       
     268        }
    274269}
Note: See TracChangeset for help on using the changeset viewer.