Changeset 1415 for trunk/src/org/expeditee/encryption
- Timestamp:
- 07/24/19 11:23:45 (5 years ago)
- Location:
- trunk/src/org/expeditee/encryption
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/encryption/io/EncryptedExpReader.java
r1414 r1415 24 24 import javax.crypto.spec.SecretKeySpec; 25 25 26 import org.expeditee.core.Point; 26 27 import org.expeditee.encryption.CryptographyConstants; 27 28 import org.expeditee.encryption.items.surrogates.EncryptionDetail; … … 33 34 import org.expeditee.io.DefaultFrameWriter; 34 35 import org.expeditee.io.ExpReader; 36 import org.expeditee.items.Constraint; 35 37 import org.expeditee.items.Item; 36 38 import org.expeditee.items.Text; … … 44 46 private SecretKey personalKey; 45 47 private boolean accessDenied = false; 48 private boolean _readingSurrogates; 49 private static final Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION); 46 50 47 51 public static boolean isEncryptedExpediteeFile(final String path) throws IOException { 48 finalBufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));49 finalString firstLine = in.readLine();52 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8")); 53 String firstLine = in.readLine(); 50 54 in.close(); 55 if (firstLine == null) return false; 51 56 return firstLine.startsWith(ENCRYPTED_EXP_FLAG); 52 57 } … … 87 92 if (accessDenied) { return null; } 88 93 94 _readingSurrogates = false; 89 95 _reader = reader; 90 96 String next = ""; … … 106 112 next = readTheConstraints(); 107 113 114 if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) { 115 // Read the surrogates 116 _readingSurrogates = true; 117 next = readTheItems(newFrame, delayedActions); 118 _readingSurrogates = false; 119 } 120 108 121 for(DelayedAction action: delayedActions) { 109 122 action.exec(); … … 124 137 } 125 138 126 // @Override127 // protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException {128 // String next = null;129 // Item currentItem = null;130 //131 // Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);132 // while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {133 // if (!isValidLine(next)) {134 // continue;135 // }136 //137 // String tag = getTagEnc(next);138 // if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {139 // currentItem = newItem(next);140 // _linePoints.put(currentItem.getID(), currentItem);141 // newFrame.addItem(currentItem);142 // currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);143 // } else if (next.startsWith("SurrogateFor")) {144 // int parentID = Integer.parseInt(next.split(" ")[1]);145 // newFrame.getItemWithID(parentID).addToSurrogates(currentItem);146 // newFrame.addToSurrogates(currentItem);147 // } else if (currentItem != null && actionShouldBeDelayed(tag.charAt(0))) {148 // delayedActions.add(new DelayedAction(currentItem, next));149 // } else if (currentItem != null) {150 // processBodyLine(currentItem, next);151 // } else {152 // System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to.");153 // }154 // }155 //156 // if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) {157 // next = readTheItems(newFrame, delayedActions);158 // }159 //160 // return next;161 // }162 163 139 protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException { 164 // Read the primary item. 165 BiConsumer<Item, String> addToBody = (item, line) -> newFrame.addItem(item); 166 String next = readLineAfterLine(false, addToBody, delayedActions); 167 168 // Read the surrogate items. 169 if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) { 170 BiConsumer<Item, String> addToSurrogates = (item, line) -> { 171 int parentID = Integer.parseInt(line.split(" ")[1]); 172 newFrame.getItemWithID(parentID).addToSurrogates(item); 173 newFrame.addToSurrogates(item); 174 }; 175 next = readLineAfterLine(true, addToSurrogates, delayedActions); 140 BiConsumer<Item, String> primaryAdd = (item, line) -> newFrame.addItem(item); 141 BiConsumer<Item, String> surrogateAdd = (item, line) -> { 142 int parentID = Integer.parseInt(line.split(" ")[1]); 143 Item parent = newFrame.getItemWithID(parentID); 144 newFrame.addToSurrogatesOnLoad(item, parent); 145 }; 146 147 if (_readingSurrogates) { 148 return readLineAfterLine(true, surrogateAdd, delayedActions); 149 } else { 150 return readLineAfterLine(false, primaryAdd, delayedActions); 151 } 152 } 153 154 @Override 155 protected String readTheConstraints() throws IOException, Exception { 156 String next = null; 157 while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) { 158 if (isValidLine(next)) { 159 Point idtype = separateValues(next.substring(2)); 160 // The next line must be the endpoints 161 if (!_reader.ready()) { 162 throw new Exception("Unexpected end of file"); 163 } 164 next = _reader.readLine(); 165 Point startend = separateValues(next.substring(2)); 166 167 Item a = _linePoints.get(startend.getX()); 168 Item b = _linePoints.get(startend.getY()); 169 170 new Constraint(a, b, idtype.getX(), idtype.getY()); 171 } 176 172 } 177 173 return next; … … 182 178 Item currentItem = null; 183 179 184 Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);185 180 while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) { 186 181 if (!isValidLine(next)) { … … 195 190 storeResult.accept(currentItem, next); 196 191 } 197 currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave); 192 EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave); 193 currentItem.setEncryptionDetailForTag(tag + "", unencryptedOnSave); 198 194 } else if (next.startsWith("SurrogateFor")) { 199 195 if (isSurrogate) { … … 212 208 } 213 209 214 // private String readLineAfterLine(Consumer<Item> storeResult, List<DelayedAction> delayedActions) throws IOException {215 // String next = null;216 // Item currentItem = null;217 //218 // Predicate<String> endOfSection = s -> {219 // return s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);220 // };221 // while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {222 // if (!isValidLine(next)) {223 // continue;224 // }225 //226 // String tag = getTagEnc(next);227 // if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {228 // currentItem = newItem(next);229 // _linePoints.put(currentItem.getID(), currentItem);230 // currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);231 // storeResult.accept(currentItem);232 // } else if (next.startsWith("SurrogateFor")) {233 // int parentID = Integer.parseInt(next.split(" ")[1]);234 // newFrame.getItemWithID(parentID).addToSurrogates(currentItem);235 // new Delayed236 // }237 // }238 //239 // return next;240 // }241 242 210 @Override 243 211 protected void processBodyLine(Item item, String line) { … … 245 213 String tag = getTagEnc(line); 246 214 String value = getValue(line); 247 248 if (item.isSurrogate() && item.isTagInherited(tag)) { 215 boolean isEncryptedLine = isEncryptedLine(line); 216 217 if (item.isSurrogate() && isEncryptedLine) { 218 // Surrogates should never have encrypted body lines. 249 219 return; 250 220 } 251 221 252 222 // Attempt to decrypt the line if necessary. 253 if (isEncryptedLine (line)) {254 LabelResult res = Label. resolveKey(item.getEncryptionLabel());223 if (isEncryptedLine) { 224 LabelResult res = Label.getLabel(item.getEncryptionLabel()); 255 225 if (res == LabelResult.SuccessResolveLabelToKey) { 256 item.setEncryptionDetailForTag(tag, EncryptionDetail.ReencryptOnSave); 226 EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave); 227 item.setEncryptionDetailForTag(tag, reencryptOnSave); 257 228 SecretKey key = new SecretKeySpec(res.key, SymmetricAlgorithm); 258 229 byte[] decryptedBytes = DecryptSymmetric(Base64.getDecoder().decode(value), key); 259 230 value = new String(decryptedBytes); 260 231 } else { 261 EncryptionDetail encryptionDetail = EncryptionDetail.UseUndecipheredValueOnSave.setUndecipheredValue(getValue(line)); 262 item.setEncryptionDetailForTag(tag, encryptionDetail); 232 EncryptionDetail undecipheredValueOnSave = new EncryptionDetail(EncryptionDetail.Type.UseUndecipheredValueOnSave); 233 undecipheredValueOnSave.setUndecipheredValue(getValue(line)); 234 item.setEncryptionDetailForTag(tag, undecipheredValueOnSave); 263 235 return; 264 236 } 265 237 } else { 266 item.setEncryptionDetailForTag(tag, EncryptionDetail.UnencryptedOnSave); 238 EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave); 239 item.setEncryptionDetailForTag(tag, unencryptedOnSave); 240 if (item.isSurrogate()) { 241 item.setTagNotInherited(tag); 242 } 267 243 } 268 244 … … 394 370 395 371 private SecretKeySpec resolveLabel(String label) { 396 LabelResult res = Label. resolveKey(label);372 LabelResult res = Label.getLabel(label); 397 373 if (res == LabelResult.SuccessResolveLabelToKey) { 398 374 byte[] keyBytes = res.key; -
trunk/src/org/expeditee/encryption/io/EncryptedExpWriter.java
r1414 r1415 23 23 import org.expeditee.encryption.CryptographyConstants; 24 24 import org.expeditee.encryption.items.surrogates.EncryptionDetail; 25 import org.expeditee.encryption.items.surrogates.EncryptionDetail.Type; 25 26 import org.expeditee.encryption.items.surrogates.Label; 26 27 import org.expeditee.encryption.items.surrogates.Label.LabelResult; 27 28 import org.expeditee.gui.Frame; 29 import org.expeditee.gui.Frame.BodyType; 28 30 import org.expeditee.gui.MessageBay; 29 31 import org.expeditee.io.Conversion; … … 62 64 } 63 65 } else { 64 LabelResult res = Label. resolveKey(label);66 LabelResult res = Label.getLabel(label); 65 67 if (res == LabelResult.SuccessResolveLabelToKey) { 66 68 byte[] keyBytes = res.key; … … 94 96 95 97 // write item 96 writeItemData(frame );98 writeItemData(frame, true); 97 99 writeTerminator(); 98 99 100 // write lines and constraints 100 101 writeLineData(); 101 102 writeTerminator(); 102 103 writeConstraintData(); 103 writeTerminator(); 104 104 105 // write surrogate items 106 if (frame.hasSurrogates()) { 107 writeSurrogateTerminator(); 108 writeItemData(frame, false); 109 writeTerminator(); 110 } else { 111 writeTerminator(); 112 } 113 105 114 writeLine(SessionStats.getFrameEventList(frame)); 106 115 } … … 126 135 127 136 protected void writeClass(Item toWrite) throws IOException { 137 if (toWrite instanceof Text && toWrite.getText().equals("Content for Label Two")) { 138 System.err.println("EncryptedExpWriter::writeClass::breakpoint"); 139 } else if (toWrite instanceof Text && toWrite.getText().equals("Encrypted") && toWrite.getPrimary().getText().equals("Content for Label One")) { 140 System.err.println("EncryptedExpWriter::writeClass::breakpoint"); 141 } 142 128 143 LinkedHashMap<Character,Method> itemTags = new LinkedHashMap<Character, Method>(getItemTags()); 129 144 LinkedHashMap<String,Method> itemTagsExt = new LinkedHashMap<String, Method>(getItemTagsExt()); … … 149 164 } 150 165 EncryptionDetail encryptionDetail = toWrite.getEncryptionDetailForTag(tag + ""); 151 152 switch(encryptionDetail) { 166 Type encryptionDetailType = encryptionDetail.getEncryptionDetailType(); 167 168 switch(encryptionDetailType) { 153 169 case UnencryptedOnSave: 154 170 writeTagUnencryptedOnSave(toWrite, param, tags, tag); … … 166 182 } 167 183 168 private void writeItemData(Frame frame ) throws IOException {184 private void writeItemData(Frame frame, boolean primaryItems) throws IOException { 169 185 // write each item in the frame 170 for (Item i : frame.getItemsToSave()) { 186 BodyType bodyType = primaryItems ? BodyType.PrimaryBody : BodyType.SurrogateBody; 187 for (Item i : frame.getItemsToSave(bodyType)) { 171 188 assert (!(i instanceof Line)); 172 189 writeItem(i); … … 176 193 assert (!(i instanceof Line)); 177 194 writeItem(i); 178 }179 180 // write out the surrogates181 List<Item> surrogates = frame.getSurrogateItemsToSave();182 if (!surrogates.isEmpty()) {183 writeSurrogateTerminator();184 for (Item i: surrogates) {185 assert (!(i instanceof Line));186 writeItem(i);187 }188 195 } 189 196 } … … 194 201 BinaryOperator<Boolean> trueExists = (a, b) -> a || b; 195 202 boolean surrogatesInherit = surrogateItems.stream().map(isTagInherited).collect(Collectors.reducing(trueExists)).get(); 196 if (surrogatesInherit && 197 Label.resolveKey(toWrite.getEncryptionLabel()) == LabelResult.SuccessResolveLabelToKey) { 198 toWrite.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave); 203 boolean userHasKey = Label.getLabel(toWrite.getEncryptionLabel()) == LabelResult.SuccessResolveLabelToKey; 204 205 if (!surrogatesInherit && userHasKey) { 206 EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave); 207 toWrite.setEncryptionDetailForTag(tag + "", reencryptOnSave); 208 writeTagReencryptOnSave(toWrite, tags, tag); 209 } else { 210 EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave); 211 toWrite.setEncryptionDetailForTag(tag + "", unencryptedOnSave); 199 212 writeTagUnencryptedOnSave(toWrite, new Object[] {}, tags, tag); 200 } else {201 toWrite.setEncryptionDetailForTag(tag + "", EncryptionDetail.ReencryptOnSave);202 writeTagReencryptOnSave(toWrite, tags, tag);203 213 } 204 214 } … … 207 217 Method toRun = tags.get(tag); 208 218 Class<?> declarer = toRun.getDeclaringClass(); 209 LabelResult res = Label. resolveKey(toWrite.getEncryptionLabel());219 LabelResult res = Label.getLabel(toWrite.getEncryptionLabel()); 210 220 if (declarer.isAssignableFrom(toWrite.getClass()) && res == LabelResult.SuccessResolveLabelToKey) { 211 221 try { -
trunk/src/org/expeditee/encryption/items/surrogates/EncryptionDetail.java
r1413 r1415 1 1 package org.expeditee.encryption.items.surrogates; 2 2 3 public enum EncryptionDetail { 4 // If the property needs to be unencrypted due to inheritance then do not try to encrypt it. 5 UnencryptedOnSave, 6 7 // If the property is not inherited by any surrogates then we can try to encrypt it. 8 // If we do not have key then default to UnencryptedOnSave. 9 ReencryptOnSave, 10 11 // If the property is no longer inherited by at least one surrogate then this value is present. 12 // If it turns out there is no longer any surrogates who inherit the value then this value can be replaced with ReencryptOnSave. 13 // Otherwise, this value must be replaced with UnencryptedOnSave. 14 InheritanceCheckOnSave, 15 16 // If we could not decrypt the value on load then EncryptionDetail.getUndecipheredValue() can be used to get the value for saving. 17 UseUndecipheredValueOnSave; 18 19 private String undecipheredValue = null; 3 public class EncryptionDetail { 4 private Type type; 5 private String undecipheredValue; 6 7 public EncryptionDetail(Type type) { 8 this.type = type; 9 } 20 10 21 11 public EncryptionDetail setUndecipheredValue(String undecipheredValue) { … … 25 15 26 16 public String getUndecipheredValue() { 27 return this.undecipheredValue; 17 return undecipheredValue; 18 } 19 20 public Type getEncryptionDetailType() { 21 return type; 22 } 23 24 @Override 25 public EncryptionDetail clone() { 26 EncryptionDetail copy = new EncryptionDetail(this.type); 27 copy.setUndecipheredValue(this.undecipheredValue); 28 return copy; 29 } 30 31 public enum Type { 32 // If the property needs to be unencrypted due to inheritance then do not try to encrypt it. 33 UnencryptedOnSave, 34 35 // If the property is not inherited by any surrogates then we can try to encrypt it. 36 // If we do not have key then default to UnencryptedOnSave. 37 ReencryptOnSave, 38 39 // If the property is no longer inherited by at least one surrogate then this value is present. 40 // If it turns out there is no longer any surrogates who inherit the value then this value can be replaced with ReencryptOnSave. 41 // Otherwise, this value must be replaced with UnencryptedOnSave. 42 InheritanceCheckOnSave, 43 44 // If we could not decrypt the value on load then EncryptionDetail.getUndecipheredValue() can be used to get the value for saving. 45 UseUndecipheredValueOnSave; 28 46 } 29 47 } -
trunk/src/org/expeditee/encryption/items/surrogates/Label.java
r1413 r1415 2 2 3 3 import java.util.ArrayList; 4 import java.util.Arrays; 4 5 import java.util.Base64; 5 import java.util.Collection;6 6 import java.util.List; 7 import java.util. Set;7 import java.util.function.Predicate; 8 8 import java.util.stream.Collectors; 9 10 import org.expeditee.auth.AuthenticatorBrowser; 11 import org.expeditee.gui.DisplayController; 12 import org.expeditee.gui.Frame; 13 import org.expeditee.gui.FrameIO; 14 import org.expeditee.gui.MessageBay; 9 import java.util.stream.Stream; 10 11 import org.expeditee.gui.ItemsList; 15 12 import org.expeditee.items.Item; 16 13 import org.expeditee.items.Text; 17 import org.expeditee.settings. UserSettings;14 import org.expeditee.settings.identity.secrets.KeyList; 18 15 19 16 public class Label { 20 17 21 private static List<LabelResult> labelCache = new ArrayList<LabelResult>(); 22 private static List<String> forbiddenLabels = new ArrayList<String>(); 23 24 public static LabelResult resolveKey(String label) { 25 26 if (forbiddenLabels.contains(label)) { 18 private static int surrogateStep = -1; 19 20 public static LabelResult getLabel(String label) { 21 Text[] userLabels = KeyList.UserLabels.get(); 22 Text labelText = null; 23 for (Text userLabel : userLabels) { 24 if (userLabel.getText().equals(label)) { 25 labelText = userLabel; 26 break; 27 } 28 } 29 30 if (labelText == null) { 31 return unableToFindLabelResult(label); 32 } else { 33 List<String> data = labelText.getData(); 34 if (data == null || data.isEmpty()) { 35 return unableToFindLabelResult(label); 36 } 37 String dataEntry = data.get(0); 38 39 if (dataEntry.contains("{")) { 40 return partialKeyResult(label, dataEntry); 41 } else { 42 return fullKeyResult(label, dataEntry); 43 } 44 } 45 } 46 47 public static void progressSurrogateMode() { 48 surrogateStep += 1; 49 } 50 51 public static void resetSurrogateMode() { 52 surrogateStep = -1; 53 } 54 55 public static List<String> getAccessibleLabelsNames(ItemsList itemsList) { 56 Text[] userLabels = KeyList.UserLabels.get(); 57 Stream<String> accessibleUserWideLabels = Arrays.asList(userLabels).stream().map(label -> label.getText()); 58 59 Predicate<Item> isLabelWithoutEncryptionLabel = item -> item.getEncryptionLabel() == null || item.getEncryptionLabel().isEmpty(); 60 itemsList.removeIf(isLabelWithoutEncryptionLabel); 61 Stream<String> accessibleFrameLocalLabels = itemsList.underlying().stream().map(item -> item.getEncryptionLabel()).distinct(); 62 63 List<String> accessibleLabelsUnion = accessibleUserWideLabels.collect(Collectors.toList()); 64 accessibleLabelsUnion.retainAll(accessibleFrameLocalLabels.collect(Collectors.toList())); 65 66 if (surrogateStep > -1 && surrogateStep < accessibleLabelsUnion.size()) { 67 String surrogateModeFilter = accessibleLabelsUnion.get(surrogateStep); 68 List<String> single = new ArrayList<String>(); 69 single.add(surrogateModeFilter); 70 return single; 71 } else { 72 resetSurrogateMode(); 73 } 74 75 return accessibleLabelsUnion; 76 } 77 78 private static LabelResult partialKeyResult(String label, String dataEntry) { 79 dataEntry = dataEntry.substring(dataEntry.indexOf('}') + 1).trim(); 80 LabelResult res = LabelResult.SuccessResolveLabelToPartialKey; 81 res.name = label; 82 return res; 83 } 84 85 private static LabelResult unableToFindLabelResult(String label) { 86 LabelResult res = LabelResult.ErrorUnableToFindLabel; 87 res.name = label; 88 return res; 89 } 90 91 private static LabelResult fullKeyResult(String label, String dataEntry) { 92 try { 93 LabelResult res = LabelResult.SuccessResolveLabelToKey; 94 res.key = Base64.getDecoder().decode(dataEntry); 95 res.name = label; 96 return res; 97 } catch (IllegalArgumentException e) { 27 98 return LabelResult.ErrorUnableToFindLabel; 28 99 } 29 30 LabelResult fromCache = getFromCache(label); 31 if (fromCache != null) { 32 return fromCache; 33 } 34 35 36 String credentialsFrameName = UserSettings.ProfileName.get() + 37 AuthenticatorBrowser.CREDENTIALS_FRAME; 38 Frame credentialsFrame = FrameIO.LoadFrame(credentialsFrameName); 39 Collection<Text> textItems = credentialsFrame.getTextItems(); 40 textItems.removeIf(t -> !t.getText().equals("Secrets")); 41 textItems.removeIf(t -> !t.hasLink()); 42 if (textItems.isEmpty()) { 43 return LabelResult.ErrorUnableToFindSecretsFrame; 44 } 45 Text linkToSecretsFrame = textItems.iterator().next(); 46 if (!linkToSecretsFrame.hasLink()) { 47 return LabelResult.ErrorUnableToFindSecretsFrame; 48 } 49 Frame secretsFrame = FrameIO.LoadFrame(linkToSecretsFrame.getParent().getFramesetName() + linkToSecretsFrame.getLink()); 50 Collection<Text> labels = secretsFrame.getTextItems(); 51 labels.removeIf(lbl -> !lbl.getText().equals(label)); 52 labels.removeIf(lbl -> lbl.getData() == null || lbl.getData().size() == 0); 53 if (labels.isEmpty()) { 54 return LabelResult.ErrorUnableToFindLabel; 55 } 56 Text labelItem = labels.iterator().next(); 57 String data = labelItem.getData().get(0); 58 if (data.contains("{")) { 59 data = data.substring(data.indexOf('}') + 1).trim(); 60 LabelResult res = LabelResult.SuccessResolveLabelToPartialKey; 61 res.key = Base64.getDecoder().decode(data); 62 return res; 63 } else { 64 try { 65 LabelResult res = LabelResult.SuccessResolveLabelToKey; 66 res.key = Base64.getDecoder().decode(data); 67 res.name = label; 68 labelCache.add(res); 69 return res; 70 } catch (IllegalArgumentException e) { 71 return LabelResult.ErrorUnableToFindLabel; 72 } 73 } 74 } 75 76 public static void progressSurrogateMode() { 77 Collection<Item> allItems = DisplayController.getCurrentFrame().getAllItems(); 78 allItems.removeIf(item -> item.getEncryptionLabel() == null); 79 MessageBay.displayMessage("Encryption labels found on items on current frame: "); 80 Set<String> allLabels = allItems.stream().map(i -> i.getEncryptionLabel()).collect(Collectors.toSet()); 81 StringBuilder sb = new StringBuilder(); 82 for (String label: allLabels) { 83 sb.append(label + ","); 84 } 85 MessageBay.displayMessage(sb.deleteCharAt(sb.length() - 1).toString()); 86 } 87 88 public static void resetSurrogateMode() { 89 // TODO Auto-generated method stub 90 91 } 92 93 private static LabelResult getFromCache(String label) { 94 List<LabelResult> cacheCanditates = labelCache.stream().filter(labelResult -> labelResult.name.equals(label)).collect(Collectors.toList()); 95 if (!cacheCanditates.isEmpty()) { 96 return cacheCanditates.get(0); 97 } else { 98 return null; 99 } 100 } 101 102 // private static List<List<String>> permutation(List<String> strings, List<List<String>> acc) { 103 // for (int i = 0; i < strings.size(); i++) { 104 // 100 } 101 102 //private static Map<LabelResult, LabelAccess> labelCache = new LinkedHashMap<LabelResult, LabelAccess>(); 103 //private static int surrogateStep = -1; 104 //private static final Predicate<? super LabelResult> isAccessible = a -> labelCache.get(a).allowed; 105 106 // public static LabelResult resolveKey(String label) { 107 // Stream<LabelResult> stream = labelCache.keySet().stream(); 108 // stream = stream.filter(res -> res.name.equals(label)); 109 // if (stream.count() == 0) { 110 // return resolveKeyWorker(label); 111 // } else { 112 // LabelResult labelResult = stream.collect(Collectors.toList()).get(0); 113 // LabelAccess accessible = labelCache.get(labelResult); 114 // if (accessible.allowed) { return labelResult; } 115 // else { return LabelResult.ErrorForbiddenLabel; } 116 // } 117 // } 118 // private static LabelResult resolveKeyWorker(String label) { 119 // Frame secretsFrame = getSecretsFrame(); 120 // if (secretsFrame == null) { 121 // return LabelResult.ErrorUnableToFindSecretsFrame; 122 // } 123 // 124 // String data = getDataFromLabel(secretsFrame, label); 125 // if (data == null) { 126 // return LabelResult.ErrorUnableToFindLabel; 127 // } 128 // 129 // if (data.contains("{")) { 130 // data = data.substring(data.indexOf('}') + 1).trim(); 131 // LabelResult res = LabelResult.SuccessResolveLabelToPartialKey; 132 // res.key = Base64.getDecoder().decode(data); 133 // return res; 134 // } else { 135 // try { 136 // LabelResult res = LabelResult.SuccessResolveLabelToKey; 137 // res.key = Base64.getDecoder().decode(data); 138 // res.name = label; 139 // labelCache.put(res, new LabelAccess(true)); 140 // return res; 141 // } catch (IllegalArgumentException e) { 142 // return LabelResult.ErrorUnableToFindLabel; 143 // } 105 144 // } 106 145 // } 146 // 147 // private static String getDataFromLabel(Frame frame, String label) { 148 // Collection<Text> labels = frame.getTextItems(); 149 // labels.removeIf(lbl -> !lbl.getText().equals(label)); 150 // labels.removeIf(lbl -> lbl.getData() == null || lbl.getData().size() == 0); 151 // if (labels.isEmpty()) { return null; } 152 // Text labelItem = labels.iterator().next(); 153 // List<String> data = labelItem.getData(); 154 // if (data.isEmpty()) { return null; } 155 // return data.get(0); 156 // } 157 // 158 // private static Frame getSecretsFrame() { 159 // String credentialsFrameName = UserSettings.ProfileName.get() + 160 // AuthenticatorBrowser.CREDENTIALS_FRAME; 161 // Frame credentialsFrame = FrameIO.LoadFrame(credentialsFrameName); 162 // Collection<Text> textItems = credentialsFrame.getTextItems(); 163 // textItems.removeIf(t -> !t.getText().equals("Secrets")); 164 // textItems.removeIf(t -> !t.hasLink()); 165 // if (textItems.isEmpty()) { 166 // return null; 167 // } 168 // Text linkToSecretsFrame = textItems.iterator().next(); 169 // if (!linkToSecretsFrame.hasLink()) { 170 // return null; 171 // } 172 // Frame secretsFrame = FrameIO.LoadFrame(linkToSecretsFrame.getParent().getFramesetName() + linkToSecretsFrame.getLink()); 173 // return secretsFrame; 174 // } 175 // 176 // private static class LabelAccess { 177 // public final boolean allowedDefault; 178 // public boolean allowed; 179 // 180 // public LabelAccess(boolean value) { 181 // allowedDefault = value; 182 // allowed = allowedDefault; 183 // } 184 // } 107 185 108 186 public enum LabelResult { … … 110 188 SuccessResolveLabelToPartialKey, 111 189 ErrorUnableToFindSecretsFrame, 112 ErrorUnableToFindLabel; 190 ErrorUnableToFindLabel, 191 ErrorForbiddenLabel; 113 192 114 193 public byte[] key; … … 125 204 return "Unable to find your Secrets Frame."; 126 205 case ErrorUnableToFindLabel: 127 return "Unable to resolve label to encrypt/decrypt frame. Label: "; 206 return "Unable to resolve label to encrypt/decrypt frame. Label: " + name; 207 case ErrorForbiddenLabel: 208 return "Whilst you have the key for label " + name + " ; you are not allowed to use it."; 128 209 } 129 210
Note:
See TracChangeset
for help on using the changeset viewer.