- Timestamp:
- 06/25/19 13:57:37 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/encryption/io/EncryptedExpWriter.java
r1410 r1413 6 6 import java.security.InvalidKeyException; 7 7 import java.security.NoSuchAlgorithmException; 8 import java.util.ArrayList;9 8 import java.util.Arrays; 10 9 import java.util.Base64; 11 10 import java.util.LinkedHashMap; 12 11 import java.util.List; 13 import java.util.Set; 14 import java.util.function.Consumer; 12 import java.util.function.BinaryOperator; 13 import java.util.function.Function; 14 import java.util.stream.Collectors; 15 15 16 16 import javax.crypto.BadPaddingException; … … 24 24 import org.expeditee.encryption.items.surrogates.EncryptionDetail; 25 25 import org.expeditee.encryption.items.surrogates.Label; 26 import org.expeditee.encryption.items.surrogates.Surrogate;27 26 import org.expeditee.encryption.items.surrogates.Label.LabelResult; 28 27 import org.expeditee.gui.Frame; … … 76 75 77 76 @Override 78 public void outputFrame(Frame frame) throws IOException {79 if (_writer == null) { return; }80 81 preOutputFrame();82 writeHeader(frame);83 84 // write item85 writeItemData(frame);86 writeTerminator();87 88 // write lines and constraints89 writeLineData();90 writeTerminator();91 writeConstraintData();92 writeTerminator();93 94 writeLine(SessionStats.getFrameEventList(frame));95 }96 97 @Override98 77 protected void preOutputFrame() { 99 78 try { … … 107 86 108 87 @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 109 108 protected void writeLine(String line) throws IOException { 110 109 // do not write empty lines … … 125 124 } 126 125 127 @Override128 protected void writeLineData() throws IOException {129 super.writeLineData();130 131 writeSurrogateLineData();132 }133 134 @Override135 126 protected void writeClass(Item toWrite) throws IOException { 136 127 LinkedHashMap<Character,Method> itemTags = new LinkedHashMap<Character, Method>(getItemTags()); 137 128 LinkedHashMap<String,Method> itemTagsExt = new LinkedHashMap<String, Method>(getItemTagsExt()); 138 129 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 145 130 writeTag(toWrite, new Object[] {}, itemTags, 'S'); 146 131 writeTag(toWrite, new Object[] {}, itemTagsExt, "_el"); 147 132 133 if (toWrite.isSurrogate()) { 134 writeLine("SurrogateFor " + toWrite.getClassic().getID()); 135 } 136 148 137 itemTags.remove('S'); 149 138 itemTagsExt.remove("_el"); 150 139 151 140 writeTags(toWrite, new Object[] {}, itemTags); 152 141 writeTags(toWrite, new Object[] {}, itemTagsExt); … … 155 144 @Override 156 145 protected <T> void writeTag(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags, T tag) { 146 if (toWrite.isSurrogate() && toWrite.isTagInherited(tag + "")) { 147 return; 148 } 157 149 EncryptionDetail encryptionDetail = toWrite.getEncryptionDetailForTag(tag + ""); 158 switch (encryptionDetail) { 150 151 switch(encryptionDetail) { 159 152 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); 161 157 break; 162 158 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); 188 160 break; 189 161 case UseUndecipheredValueOnSave: 190 try { 191 writeLine(tag + "E", encryptionDetail.getUndecipheredValue()); 192 } catch (IOException e) { 193 e.printStackTrace(); 194 } 162 writeTagUseUndecipheredValueOnSave(toWrite, tags, tag); 195 163 break; 196 164 } … … 204 172 } 205 173 206 for ( finalItem i: frame.getBodyItemsWithInsufficientPermissions()) {174 for (Item i: frame.getBodyItemsWithInsufficientPermissions()) { 207 175 assert (!(i instanceof Line)); 208 176 writeItem(i); 209 177 } 210 178 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(); 221 180 if (!surrogates.isEmpty()) { 222 // If there is some surrogates, write the surrogate223 // terminator, then write out the surrogates.224 181 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); 253 241 } 254 242 255 243 private void writeSurrogateTerminator() throws IOException { 256 244 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()); 257 252 } 258 253 … … 271 266 return null; 272 267 } 273 } 268 } 274 269 }
Note:
See TracChangeset
for help on using the changeset viewer.