Changeset 1202 for trunk/src/org/expeditee/auth/EncryptedExpReader.java
- Timestamp:
- 01/30/19 12:49:25 (5 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/auth/EncryptedExpReader.java
r1201 r1202 1 package org.expeditee.auth .io;1 package org.expeditee.auth; 2 2 3 3 import java.io.BufferedReader; 4 4 import java.io.FileInputStream; 5 import java.io.FileReader; 5 6 import java.io.IOException; 6 7 import java.io.InputStreamReader; … … 19 20 import javax.crypto.NoSuchPaddingException; 20 21 import javax.crypto.SecretKey; 21 22 import org.expeditee.auth.Authenticator; 22 import javax.crypto.spec.SecretKeySpec; 23 23 24 import org.expeditee.gui.Frame; 24 25 import org.expeditee.io.ExpReader; 26 import org.expeditee.items.Text; 27 import org.expeditee.settings.auth.secrets.KeyList; 25 28 import org.ngikm.Util.ThrowingFunction; 26 29 import org.ngikm.cryptography.CryptographyConstants; … … 28 31 public class EncryptedExpReader extends ExpReader implements CryptographyConstants { 29 32 static final String ENCRYPTED_EXP_FLAG = "EncryptedExp"; 30 private List<SecretKey> keys; 31 32 public EncryptedExpReader(final String frameName) throws UnsupportedEncodingException { 33 private SecretKey personalKey; 34 private List<SecretKey> multiKey; 35 private boolean accessDenied = false; 36 private boolean usePersonalKey; 37 38 public EncryptedExpReader(final String frameName, final boolean usePersonalKey) throws UnsupportedEncodingException { 33 39 super(frameName); 40 this.usePersonalKey = usePersonalKey; 34 41 } 35 42 … … 41 48 } 42 49 50 public int getVersionEnc(String fullpath) { 51 try { 52 BufferedReader reader; 53 if (usePersonalKey) { 54 reader = new EncryptedProfileLineReader(new BufferedReader(new FileReader(fullpath))); 55 } else { 56 reader = new EncryptedLineReader(new BufferedReader(new FileReader(fullpath))); 57 } 58 String next = ""; 59 // First read the header lines until we get the version number 60 while (reader.ready() && !(next = reader.readLine()).equals("Z")) { 61 if (isValidLine(next)) { 62 Character tag = getTag(next); 63 String value = getValue(next); 64 if (tag.equals('V')) { 65 reader.close(); 66 return Integer.parseInt(value); 67 } 68 } 69 } 70 reader.close(); 71 } catch (Exception e) { 72 } 73 return -1; 74 } 75 43 76 @Override 44 77 public Frame readFrame(final String fullPath) throws IOException { 45 78 final Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8"); 46 return readFrame(new EncryptedLineReader(new BufferedReader(in))); 47 } 48 49 public class EncryptedLineReader extends BufferedReader { 50 51 public EncryptedLineReader(final Reader in) { 52 super(in); 53 } 54 55 @Override 56 public String readLine() throws IOException { 57 final String line = super.readLine(); 58 if (line.isEmpty()) { return ""; } 59 if (line.startsWith(ENCRYPTED_EXP_FLAG)) { 60 try { 61 // resolve labels to secret keys 62 final List<String> labels = Arrays.asList(line.split(" ")).stream().skip(1).collect(Collectors.toList()); 63 final Authenticator auth = new Authenticator(); 64 final ThrowingFunction<String, SecretKey, Exception> worker = new ThrowingFunction<String, SecretKey, Exception>() { 65 @Override 66 public SecretKey applyThrows(final String label) throws Exception { 67 return auth.getSecretKey(label, System.getProperty("password")); 68 } 69 }; 70 keys = labels.stream().map(worker).collect(Collectors.toList()); 71 return readLine(); 72 } catch (final Exception e) { 73 return ""; 74 } 75 } 76 77 if (keys == null) { 78 // We do not have permission to view this frame. 79 return ""; 80 } 81 82 // use keys to decrypt 83 byte[] toDecrypt = Base64.getDecoder().decode(line); 84 for (final SecretKey key: keys) { 85 toDecrypt = DecryptSymmetric(toDecrypt, key); 86 } 87 final String result = new String(toDecrypt); 88 if (result.startsWith("Z")) { return result.trim(); } 89 else { return result; } 90 } 79 if (usePersonalKey) { 80 return readFrame(new EncryptedProfileLineReader(new BufferedReader(in))); 81 } else { 82 return readFrame(new EncryptedLineReader(new BufferedReader(in))); 83 } 84 } 85 86 @Override 87 public Frame readFrame(final BufferedReader reader) throws IOException { 88 if (accessDenied) { return null; } 89 else return super.readFrame(reader); 91 90 } 92 91 … … 111 110 } 112 111 } 112 113 private class EncryptedProfileLineReader extends BufferedReader { 114 115 public EncryptedProfileLineReader(final Reader in) { 116 super(in); 117 } 118 119 @Override 120 /** 121 * Reads a line from an encrypted exp file that uses profile encryption (single key; personal key) 122 * Returns that line to process, null if the currently logged in users personal key is not the appropriate one (access denied). 123 */ 124 public String readLine() throws IOException { 125 // read encrypted line 126 final String line = super.readLine(); 127 128 if (line.isEmpty()) { return ""; } 129 if (line.startsWith(ENCRYPTED_EXP_FLAG)) { 130 // record/overwrite previous personal key then ignore this line by recursing 131 final Text text = KeyList.PersonalKey.get(); 132 final byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0)); 133 personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 134 return readLine(); 135 } 136 137 // decrypt line and return result 138 final byte[] toDecrypt = Base64.getDecoder().decode(line); 139 final byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey); 140 if (decrypted == null) { 141 accessDenied = true; 142 return null; // access denied 143 } else { 144 final String decryptedLine = new String(decrypted); 145 if (decryptedLine.startsWith("Z")) { return decryptedLine.trim(); } 146 else { return decryptedLine; } 147 } 148 } 149 } 150 151 private class EncryptedLineReader extends BufferedReader { 152 153 public EncryptedLineReader(final Reader in) { 154 super(in); 155 } 156 157 @Override 158 /** 159 * Reads a line from an encrypted exp file that uses (potentially multiple) labeled keys 160 * Returns that line to process, null if the ... 161 */ 162 public String readLine() throws IOException { 163 // read encrypted line 164 final String line = super.readLine(); 165 166 if (line.isEmpty()) { return ""; } 167 if (line.startsWith(ENCRYPTED_EXP_FLAG)) { 168 // resolve labels to secret keys 169 final List<String> labels = Arrays.asList(line.split(" ")).stream().skip(1).collect(Collectors.toList()); 170 final ThrowingFunction<String, SecretKey, Exception> worker = new ThrowingFunction<String, SecretKey, Exception>() { 171 @Override 172 public SecretKey applyThrows(final String label) throws Exception { 173 return Authenticator.getInstance().getSecretKey(label, System.getProperty("password")); 174 } 175 }; 176 multiKey = labels.stream().map(l -> { 177 try { 178 return worker.apply(l); 179 } catch (final Exception e) { 180 return null; 181 } 182 }).collect(Collectors.toList()); 183 184 // confirm you have all the keys necessary for decryption 185 if (multiKey.contains(null)) { 186 return null; 187 } 188 189 // move onto the next line 190 return readLine(); 191 } 192 193 // decrypt line and return result 194 final byte[] toDecrypt = Base64.getDecoder().decode(line); 195 byte[] decryptedBytes = null; 196 for (final SecretKey key: multiKey) { 197 decryptedBytes = DecryptSymmetric(toDecrypt, key); 198 if (decryptedBytes == null) { return null; } 199 } 200 final String decrypted = new String(decryptedBytes); 201 if (decrypted.startsWith("Z")) { return decrypted.trim(); } 202 else { return decrypted; } 203 } 204 } 113 205 }
Note:
See TracChangeset
for help on using the changeset viewer.