[1399] | 1 | package org.expeditee.encryption.io;
|
---|
[1200] | 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.io.FileInputStream;
|
---|
[1202] | 5 | import java.io.FileReader;
|
---|
[1200] | 6 | import java.io.IOException;
|
---|
| 7 | import java.io.InputStreamReader;
|
---|
| 8 | import java.io.Reader;
|
---|
[1408] | 9 | import java.lang.reflect.Method;
|
---|
[1200] | 10 | import java.security.InvalidKeyException;
|
---|
| 11 | import java.security.NoSuchAlgorithmException;
|
---|
[1408] | 12 | import java.util.ArrayList;
|
---|
[1200] | 13 | import java.util.Arrays;
|
---|
| 14 | import java.util.Base64;
|
---|
[1408] | 15 | import java.util.List;
|
---|
[1414] | 16 | import java.util.function.BiConsumer;
|
---|
[1408] | 17 | import java.util.function.Predicate;
|
---|
[1200] | 18 |
|
---|
| 19 | import javax.crypto.BadPaddingException;
|
---|
| 20 | import javax.crypto.Cipher;
|
---|
| 21 | import javax.crypto.IllegalBlockSizeException;
|
---|
| 22 | import javax.crypto.NoSuchPaddingException;
|
---|
| 23 | import javax.crypto.SecretKey;
|
---|
[1202] | 24 | import javax.crypto.spec.SecretKeySpec;
|
---|
[1200] | 25 |
|
---|
[1415] | 26 | import org.expeditee.core.Point;
|
---|
[1389] | 27 | import org.expeditee.encryption.CryptographyConstants;
|
---|
[1408] | 28 | import org.expeditee.encryption.items.surrogates.EncryptionDetail;
|
---|
[1409] | 29 | import org.expeditee.encryption.items.surrogates.Label;
|
---|
| 30 | import org.expeditee.encryption.items.surrogates.Label.LabelResult;
|
---|
[1200] | 31 | import org.expeditee.gui.Frame;
|
---|
[1408] | 32 | import org.expeditee.gui.FrameIO;
|
---|
| 33 | import org.expeditee.io.Conversion;
|
---|
[1414] | 34 | import org.expeditee.io.DefaultFrameWriter;
|
---|
[1200] | 35 | import org.expeditee.io.ExpReader;
|
---|
[1415] | 36 | import org.expeditee.items.Constraint;
|
---|
[1400] | 37 | import org.expeditee.items.Item;
|
---|
[1202] | 38 | import org.expeditee.items.Text;
|
---|
[1243] | 39 | import org.expeditee.settings.identity.secrets.KeyList;
|
---|
[1200] | 40 |
|
---|
| 41 | public class EncryptedExpReader extends ExpReader implements CryptographyConstants {
|
---|
[1413] | 42 |
|
---|
[1408] | 43 | private static final String ENCRYPTED_EXP_FLAG = "EncryptedExp";
|
---|
| 44 | private static final String labelProfile = "Profile";
|
---|
| 45 | private static final String labelNone = "None";
|
---|
[1202] | 46 | private SecretKey personalKey;
|
---|
| 47 | private boolean accessDenied = false;
|
---|
[1415] | 48 | private boolean _readingSurrogates;
|
---|
| 49 | private static final Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);
|
---|
[1227] | 50 |
|
---|
[1200] | 51 | public static boolean isEncryptedExpediteeFile(final String path) throws IOException {
|
---|
[1415] | 52 | BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
|
---|
| 53 | String firstLine = in.readLine();
|
---|
[1200] | 54 | in.close();
|
---|
[1415] | 55 | if (firstLine == null) return false;
|
---|
[1200] | 56 | return firstLine.startsWith(ENCRYPTED_EXP_FLAG);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[1413] | 59 | public EncryptedExpReader(String frameName) {
|
---|
| 60 | super(frameName);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[1202] | 63 | public int getVersionEnc(String fullpath) {
|
---|
| 64 | try {
|
---|
[1227] | 65 | BufferedReader reader = new EncryptedLineReader(new BufferedReader(new FileReader(fullpath)));
|
---|
[1202] | 66 | String next = "";
|
---|
| 67 | // First read the header lines until we get the version number
|
---|
| 68 | while (reader.ready() && !(next = reader.readLine()).equals("Z")) {
|
---|
| 69 | if (isValidLine(next)) {
|
---|
| 70 | Character tag = getTag(next);
|
---|
| 71 | String value = getValue(next);
|
---|
| 72 | if (tag.equals('V')) {
|
---|
| 73 | reader.close();
|
---|
| 74 | return Integer.parseInt(value);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | reader.close();
|
---|
| 79 | } catch (Exception e) {
|
---|
| 80 | }
|
---|
| 81 | return -1;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[1200] | 84 | @Override
|
---|
| 85 | public Frame readFrame(final String fullPath) throws IOException {
|
---|
[1227] | 86 | Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8");
|
---|
| 87 | return readFrame(new EncryptedLineReader(in));
|
---|
[1200] | 88 | }
|
---|
| 89 |
|
---|
[1202] | 90 | @Override
|
---|
[1408] | 91 | public Frame readFrame(BufferedReader reader) throws IOException {
|
---|
[1202] | 92 | if (accessDenied) { return null; }
|
---|
[1408] | 93 |
|
---|
[1415] | 94 | _readingSurrogates = false;
|
---|
[1408] | 95 | _reader = reader;
|
---|
| 96 | String next = "";
|
---|
| 97 | Frame newFrame = new Frame();
|
---|
| 98 | List<DelayedAction> delayedActions = new ArrayList<DelayedAction>();
|
---|
| 99 | newFrame.setName(_frameName);
|
---|
| 100 |
|
---|
| 101 | try {
|
---|
| 102 | // First read all the header lines
|
---|
| 103 | next = readTheHeader(newFrame);
|
---|
| 104 |
|
---|
| 105 | // Now read all the items
|
---|
| 106 | next = readTheItems(newFrame, delayedActions);
|
---|
| 107 |
|
---|
| 108 | // Read the lines
|
---|
| 109 | next = readTheLines(newFrame);
|
---|
| 110 |
|
---|
| 111 | // Read the constraints
|
---|
| 112 | next = readTheConstraints();
|
---|
| 113 |
|
---|
[1415] | 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 |
|
---|
[1408] | 121 | for(DelayedAction action: delayedActions) {
|
---|
| 122 | action.exec();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | // Read the stats
|
---|
| 126 | next = readTheStats(newFrame);
|
---|
| 127 | } catch (Exception e) {
|
---|
| 128 | e.printStackTrace();
|
---|
| 129 | System.out.println("Error reading frame file line: " + next + " " + e.getMessage());
|
---|
[1400] | 130 | }
|
---|
[1408] | 131 |
|
---|
| 132 | _reader.close();
|
---|
| 133 | FrameIO.setSavedProperties(newFrame);
|
---|
| 134 | newFrame.setChanged(false);
|
---|
| 135 |
|
---|
| 136 | return newFrame;
|
---|
[1200] | 137 | }
|
---|
| 138 |
|
---|
[1408] | 139 | protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException {
|
---|
[1415] | 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 | };
|
---|
[1414] | 146 |
|
---|
[1415] | 147 | if (_readingSurrogates) {
|
---|
| 148 | return readLineAfterLine(true, surrogateAdd, delayedActions);
|
---|
| 149 | } else {
|
---|
| 150 | return readLineAfterLine(false, primaryAdd, delayedActions);
|
---|
[1414] | 151 | }
|
---|
[1415] | 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 | }
|
---|
| 172 | }
|
---|
[1414] | 173 | return next;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | private String readLineAfterLine(boolean isSurrogate, BiConsumer<Item, String> storeResult, List<DelayedAction> delayedActions) throws IOException {
|
---|
[1408] | 177 | String next = null;
|
---|
| 178 | Item currentItem = null;
|
---|
[1400] | 179 |
|
---|
[1408] | 180 | while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {
|
---|
| 181 | if (!isValidLine(next)) {
|
---|
| 182 | continue;
|
---|
| 183 | }
|
---|
[1413] | 184 |
|
---|
[1408] | 185 | String tag = getTagEnc(next);
|
---|
[1414] | 186 | if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {
|
---|
[1408] | 187 | currentItem = newItem(next);
|
---|
| 188 | _linePoints.put(currentItem.getID(), currentItem);
|
---|
[1414] | 189 | if (!isSurrogate) {
|
---|
| 190 | storeResult.accept(currentItem, next);
|
---|
| 191 | }
|
---|
[1415] | 192 | EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
|
---|
| 193 | currentItem.setEncryptionDetailForTag(tag + "", unencryptedOnSave);
|
---|
[1413] | 194 | } else if (next.startsWith("SurrogateFor")) {
|
---|
[1414] | 195 | if (isSurrogate) {
|
---|
| 196 | storeResult.accept(currentItem, next);
|
---|
| 197 | }
|
---|
[1408] | 198 | } else if (currentItem != null && actionShouldBeDelayed(tag.charAt(0))) {
|
---|
| 199 | delayedActions.add(new DelayedAction(currentItem, next));
|
---|
| 200 | } else if (currentItem != null) {
|
---|
| 201 | processBodyLine(currentItem, next);
|
---|
| 202 | } else {
|
---|
| 203 | System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to.");
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | return next;
|
---|
| 208 | }
|
---|
[1414] | 209 |
|
---|
[1408] | 210 | @Override
|
---|
| 211 | protected void processBodyLine(Item item, String line) {
|
---|
| 212 | // separate the tag from the value
|
---|
| 213 | String tag = getTagEnc(line);
|
---|
| 214 | String value = getValue(line);
|
---|
[1415] | 215 | boolean isEncryptedLine = isEncryptedLine(line);
|
---|
[1408] | 216 |
|
---|
[1415] | 217 | if (item.isSurrogate() && isEncryptedLine) {
|
---|
| 218 | // Surrogates should never have encrypted body lines.
|
---|
[1413] | 219 | return;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[1408] | 222 | // Attempt to decrypt the line if necessary.
|
---|
[1415] | 223 | if (isEncryptedLine) {
|
---|
| 224 | LabelResult res = Label.getLabel(item.getEncryptionLabel());
|
---|
[1408] | 225 | if (res == LabelResult.SuccessResolveLabelToKey) {
|
---|
[1415] | 226 | EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave);
|
---|
| 227 | item.setEncryptionDetailForTag(tag, reencryptOnSave);
|
---|
[1408] | 228 | SecretKey key = new SecretKeySpec(res.key, SymmetricAlgorithm);
|
---|
| 229 | byte[] decryptedBytes = DecryptSymmetric(Base64.getDecoder().decode(value), key);
|
---|
| 230 | value = new String(decryptedBytes);
|
---|
| 231 | } else {
|
---|
[1415] | 232 | EncryptionDetail undecipheredValueOnSave = new EncryptionDetail(EncryptionDetail.Type.UseUndecipheredValueOnSave);
|
---|
| 233 | undecipheredValueOnSave.setUndecipheredValue(getValue(line));
|
---|
| 234 | item.setEncryptionDetailForTag(tag, undecipheredValueOnSave);
|
---|
[1408] | 235 | return;
|
---|
| 236 | }
|
---|
| 237 | } else {
|
---|
[1415] | 238 | EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
|
---|
| 239 | item.setEncryptionDetailForTag(tag, unencryptedOnSave);
|
---|
| 240 | if (item.isSurrogate()) {
|
---|
| 241 | item.setTagNotInherited(tag);
|
---|
| 242 | }
|
---|
[1408] | 243 | }
|
---|
| 244 |
|
---|
| 245 | // Process the line
|
---|
| 246 | Method toRun = tag.startsWith("_") ? _ItemTagsExt.get(tag) : _ItemTags.get(tag.charAt(0));
|
---|
| 247 | if (toRun == null) {
|
---|
| 248 | System.out.println("Error accessing tag method: " + tag);
|
---|
| 249 | }
|
---|
| 250 | Object[] vals = Conversion.Convert(toRun, value);
|
---|
| 251 | try {
|
---|
| 252 | if (vals != null) {
|
---|
| 253 | toRun.invoke(item, vals);
|
---|
| 254 | }
|
---|
| 255 | } catch (Exception e) {
|
---|
| 256 | System.out.println("Error running tag method: " + tag);
|
---|
| 257 | e.printStackTrace();
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
[1413] | 260 |
|
---|
| 261 | protected static String getValue(String line) {
|
---|
| 262 | String[] split = line.split(" ");
|
---|
| 263 | if (split.length >= 2) {
|
---|
| 264 | return line.substring(split[0].length()).trim();
|
---|
| 265 | } else {
|
---|
| 266 | return null;
|
---|
[1400] | 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[1413] | 270 | private static String getTagEnc(String line) {
|
---|
[1408] | 271 | char charAtZero = line.charAt(0);
|
---|
| 272 | if (charAtZero == '_') {
|
---|
| 273 | return line.split(" ")[0];
|
---|
| 274 | } else {
|
---|
| 275 | return charAtZero + "";
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
[1413] | 278 |
|
---|
| 279 | private static boolean isEncryptedLine(String line) {
|
---|
[1408] | 280 | if (line.startsWith("S") || line.startsWith("_el")) {
|
---|
| 281 | return false;
|
---|
| 282 | }
|
---|
| 283 | if (line.length() > 2) {
|
---|
| 284 | return line.charAt(1) == 'E';
|
---|
| 285 | } else {
|
---|
| 286 | return false;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[1200] | 290 | private static byte[] DecryptSymmetric(final byte[] toDecrypt, final SecretKey key) {
|
---|
| 291 | try {
|
---|
| 292 | final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
|
---|
| 293 | cipher.init(Cipher.DECRYPT_MODE, key);
|
---|
| 294 | final byte[] decryptedBytes = cipher.doFinal(toDecrypt);
|
---|
| 295 | int indexOfZero = decryptedBytes.length - 1;
|
---|
| 296 | for (int i = decryptedBytes.length - 1; i >= 0; i--) {
|
---|
| 297 | if (decryptedBytes[i] != (byte) 0) {
|
---|
| 298 | indexOfZero = i + 1;
|
---|
| 299 | break;
|
---|
| 300 | }
|
---|
| 301 | }
|
---|
| 302 | if (indexOfZero < 0) { return decryptedBytes; }
|
---|
| 303 | else { return Arrays.copyOf(decryptedBytes, indexOfZero); }
|
---|
| 304 | } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
---|
| 305 | | IllegalBlockSizeException | BadPaddingException e) {
|
---|
| 306 | e.printStackTrace();
|
---|
| 307 | return null;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
[1413] | 310 |
|
---|
[1227] | 311 | private class EncryptedLineReader extends BufferedReader {
|
---|
[1408] | 312 | private boolean noneMode = false;
|
---|
| 313 |
|
---|
[1227] | 314 | public EncryptedLineReader(Reader in) {
|
---|
[1202] | 315 | super(in);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | @Override
|
---|
| 319 | /**
|
---|
[1227] | 320 | * Reads a line from an encrypted exp file that uses an encryption specified by the first line of the file.
|
---|
| 321 | * Returns that line to process, null if the currently logged in users doesn't own the appropriate key (access denied).
|
---|
[1202] | 322 | */
|
---|
| 323 | public String readLine() throws IOException {
|
---|
[1227] | 324 | String line = super.readLine();
|
---|
[1202] | 325 |
|
---|
[1227] | 326 | if (line.isEmpty()) {
|
---|
| 327 | return "";
|
---|
| 328 | }
|
---|
[1408] | 329 |
|
---|
| 330 | if (noneMode) {
|
---|
| 331 | return line;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[1202] | 334 | if (line.startsWith(ENCRYPTED_EXP_FLAG)) {
|
---|
[1227] | 335 | String label = line.replace(ENCRYPTED_EXP_FLAG, "");
|
---|
| 336 | // if using Profile label, use personal key
|
---|
[1408] | 337 | if (label.equals(labelProfile)) {
|
---|
[1227] | 338 | Text text = KeyList.PersonalKey.get();
|
---|
| 339 | byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0));
|
---|
| 340 | personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
| 341 | return readLine();
|
---|
[1408] | 342 | } else if (label.equals(labelNone)) {
|
---|
| 343 | noneMode = true;
|
---|
| 344 | return readLine();
|
---|
[1227] | 345 | } else {
|
---|
| 346 | personalKey = resolveLabel(label);
|
---|
[1323] | 347 | if (personalKey == null) {
|
---|
| 348 | return null;
|
---|
| 349 | } else {
|
---|
| 350 | return readLine();
|
---|
| 351 | }
|
---|
[1227] | 352 | }
|
---|
[1202] | 353 | }
|
---|
| 354 |
|
---|
| 355 | // decrypt line and return result
|
---|
[1227] | 356 | byte[] toDecrypt = Base64.getDecoder().decode(line);
|
---|
| 357 | byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey);
|
---|
[1202] | 358 | if (decrypted == null) {
|
---|
| 359 | accessDenied = true;
|
---|
| 360 | return null; // access denied
|
---|
| 361 | } else {
|
---|
[1227] | 362 | String decryptedLine = new String(decrypted);
|
---|
| 363 | if (decryptedLine.startsWith("Z")) {
|
---|
| 364 | return decryptedLine.trim();
|
---|
| 365 | } else {
|
---|
| 366 | return decryptedLine;
|
---|
| 367 | }
|
---|
[1202] | 368 | }
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[1227] | 371 | private SecretKeySpec resolveLabel(String label) {
|
---|
[1415] | 372 | LabelResult res = Label.getLabel(label);
|
---|
[1401] | 373 | if (res == LabelResult.SuccessResolveLabelToKey) {
|
---|
| 374 | byte[] keyBytes = res.key;
|
---|
| 375 | return new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
| 376 | }
|
---|
| 377 | return null;
|
---|
[1202] | 378 | }
|
---|
| 379 | }
|
---|
[1200] | 380 | }
|
---|