1 | package org.expeditee.auth;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileReader;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.io.Reader;
|
---|
9 | import java.io.UnsupportedEncodingException;
|
---|
10 | import java.security.InvalidKeyException;
|
---|
11 | import java.security.NoSuchAlgorithmException;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Base64;
|
---|
14 |
|
---|
15 | import javax.crypto.BadPaddingException;
|
---|
16 | import javax.crypto.Cipher;
|
---|
17 | import javax.crypto.IllegalBlockSizeException;
|
---|
18 | import javax.crypto.NoSuchPaddingException;
|
---|
19 | import javax.crypto.SecretKey;
|
---|
20 | import javax.crypto.spec.SecretKeySpec;
|
---|
21 |
|
---|
22 | import org.expeditee.encryption.CryptographyConstants;
|
---|
23 | import org.expeditee.gui.Frame;
|
---|
24 | import org.expeditee.io.ExpReader;
|
---|
25 | import org.expeditee.items.Text;
|
---|
26 | import org.expeditee.settings.identity.secrets.KeyList;
|
---|
27 |
|
---|
28 | public class EncryptedExpReader extends ExpReader implements CryptographyConstants {
|
---|
29 | static final String ENCRYPTED_EXP_FLAG = "EncryptedExp";
|
---|
30 | private SecretKey personalKey;
|
---|
31 | private boolean accessDenied = false;
|
---|
32 |
|
---|
33 | public EncryptedExpReader(final String frameName) throws UnsupportedEncodingException {
|
---|
34 | super(frameName);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static boolean isEncryptedExpediteeFile(final String path) throws IOException {
|
---|
38 | final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
|
---|
39 | final String firstLine = in.readLine();
|
---|
40 | in.close();
|
---|
41 | return firstLine.startsWith(ENCRYPTED_EXP_FLAG);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public int getVersionEnc(String fullpath) {
|
---|
45 | try {
|
---|
46 | BufferedReader reader = new EncryptedLineReader(new BufferedReader(new FileReader(fullpath)));
|
---|
47 | String next = "";
|
---|
48 | // First read the header lines until we get the version number
|
---|
49 | while (reader.ready() && !(next = reader.readLine()).equals("Z")) {
|
---|
50 | if (isValidLine(next)) {
|
---|
51 | Character tag = getTag(next);
|
---|
52 | String value = getValue(next);
|
---|
53 | if (tag.equals('V')) {
|
---|
54 | reader.close();
|
---|
55 | return Integer.parseInt(value);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | reader.close();
|
---|
60 | } catch (Exception e) {
|
---|
61 | }
|
---|
62 | return -1;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public Frame readFrame(final String fullPath) throws IOException {
|
---|
67 | Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8");
|
---|
68 | return readFrame(new EncryptedLineReader(in));
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public Frame readFrame(final BufferedReader reader) throws IOException {
|
---|
73 | if (accessDenied) { return null; }
|
---|
74 | else return super.readFrame(reader);
|
---|
75 | }
|
---|
76 |
|
---|
77 | private static byte[] DecryptSymmetric(final byte[] toDecrypt, final SecretKey key) {
|
---|
78 | try {
|
---|
79 | final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
|
---|
80 | cipher.init(Cipher.DECRYPT_MODE, key);
|
---|
81 | final byte[] decryptedBytes = cipher.doFinal(toDecrypt);
|
---|
82 | int indexOfZero = decryptedBytes.length - 1;
|
---|
83 | for (int i = decryptedBytes.length - 1; i >= 0; i--) {
|
---|
84 | if (decryptedBytes[i] != (byte) 0) {
|
---|
85 | indexOfZero = i + 1;
|
---|
86 | break;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | if (indexOfZero < 0) { return decryptedBytes; }
|
---|
90 | else { return Arrays.copyOf(decryptedBytes, indexOfZero); }
|
---|
91 | } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
---|
92 | | IllegalBlockSizeException | BadPaddingException e) {
|
---|
93 | e.printStackTrace();
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private class EncryptedLineReader extends BufferedReader {
|
---|
99 | public EncryptedLineReader(Reader in) {
|
---|
100 | super(in);
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | /**
|
---|
105 | * Reads a line from an encrypted exp file that uses an encryption specified by the first line of the file.
|
---|
106 | * Returns that line to process, null if the currently logged in users doesn't own the appropriate key (access denied).
|
---|
107 | */
|
---|
108 | public String readLine() throws IOException {
|
---|
109 | String line = super.readLine();
|
---|
110 |
|
---|
111 | if (line.isEmpty()) {
|
---|
112 | return "";
|
---|
113 | }
|
---|
114 | if (line.startsWith(ENCRYPTED_EXP_FLAG)) {
|
---|
115 | String label = line.replace(ENCRYPTED_EXP_FLAG, "");
|
---|
116 | // if using Profile label, use personal key
|
---|
117 | if (label.startsWith("Profile")) {
|
---|
118 | Text text = KeyList.PersonalKey.get();
|
---|
119 | byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0));
|
---|
120 | personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
121 | return readLine();
|
---|
122 | } else {
|
---|
123 | personalKey = resolveLabel(label);
|
---|
124 | if (personalKey == null) {
|
---|
125 | return null;
|
---|
126 | } else {
|
---|
127 | return readLine();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | // decrypt line and return result
|
---|
133 | byte[] toDecrypt = Base64.getDecoder().decode(line);
|
---|
134 | byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey);
|
---|
135 | if (decrypted == null) {
|
---|
136 | accessDenied = true;
|
---|
137 | return null; // access denied
|
---|
138 | } else {
|
---|
139 | String decryptedLine = new String(decrypted);
|
---|
140 | if (decryptedLine.startsWith("Z")) {
|
---|
141 | return decryptedLine.trim();
|
---|
142 | } else {
|
---|
143 | return decryptedLine;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | private SecretKeySpec resolveLabel(String label) {
|
---|
149 | byte[] keyBytes = EncryptedExpWriter.resolveKeyFromLabel(label, "");
|
---|
150 | return new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|