Changeset 1227 for trunk/src/org/expeditee/auth/EncryptedExpReader.java
- Timestamp:
- 01/31/19 16:34:06 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/auth/EncryptedExpReader.java
r1202 r1227 12 12 import java.util.Arrays; 13 13 import java.util.Base64; 14 import java.util.List;15 import java.util.stream.Collectors;16 14 17 15 import javax.crypto.BadPaddingException; … … 26 24 import org.expeditee.items.Text; 27 25 import org.expeditee.settings.auth.secrets.KeyList; 28 import org.ngikm.Util.ThrowingFunction;29 26 import org.ngikm.cryptography.CryptographyConstants; 27 28 import sun.reflect.generics.reflectiveObjects.NotImplementedException; 30 29 31 30 public class EncryptedExpReader extends ExpReader implements CryptographyConstants { 32 31 static final String ENCRYPTED_EXP_FLAG = "EncryptedExp"; 33 32 private SecretKey personalKey; 34 private List<SecretKey> multiKey;33 //private List<SecretKey> multiKey; 35 34 private boolean accessDenied = false; 36 private boolean usePersonalKey;37 38 public EncryptedExpReader(final String frameName , final boolean usePersonalKey) throws UnsupportedEncodingException {35 //private boolean usePersonalKey; 36 37 public EncryptedExpReader(final String frameName) throws UnsupportedEncodingException { 39 38 super(frameName); 40 this.usePersonalKey = usePersonalKey;41 39 } 42 40 … … 50 48 public int getVersionEnc(String fullpath) { 51 49 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 }50 BufferedReader reader = new EncryptedLineReader(new BufferedReader(new FileReader(fullpath))); 51 //if (usePersonalKey) { 52 // reader = new EncryptedProfileLineReader(new BufferedReader(new FileReader(fullpath))); 53 //} else { 54 // reader = new EncryptedLineReader(new BufferedReader(new FileReader(fullpath))); 55 //} 58 56 String next = ""; 59 57 // First read the header lines until we get the version number … … 76 74 @Override 77 75 public Frame readFrame(final String fullPath) throws IOException { 78 final Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8"); 79 if (usePersonalKey) { 80 return readFrame(new EncryptedProfileLineReader(new BufferedReader(in))); 81 } else { 82 return readFrame(new EncryptedLineReader(new BufferedReader(in))); 83 } 76 Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8"); 77 return readFrame(new EncryptedLineReader(in)); 78 //if (usePersonalKey) { 79 // return readFrame(new EncryptedProfileLineReader(new BufferedReader(in))); 80 //} else { 81 // return readFrame(new EncryptedLineReader(new BufferedReader(in))); 82 //} 84 83 } 85 84 … … 111 110 } 112 111 113 private class EncryptedProfileLineReader extends BufferedReader { 114 115 public EncryptedProfileLineReader(final Reader in) { 112 private class EncryptedLineReader extends BufferedReader { 113 public EncryptedLineReader(Reader in) { 116 114 super(in); 117 115 } … … 119 117 @Override 120 118 /** 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).119 * Reads a line from an encrypted exp file that uses an encryption specified by the first line of the file. 120 * Returns that line to process, null if the currently logged in users doesn't own the appropriate key (access denied). 123 121 */ 124 122 public String readLine() throws IOException { 125 // read encrypted line 126 final String line = super.readLine(); 123 String line = super.readLine(); 127 124 128 if (line.isEmpty()) { return ""; } 125 if (line.isEmpty()) { 126 return ""; 127 } 129 128 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(); 129 String label = line.replace(ENCRYPTED_EXP_FLAG, ""); 130 // if using Profile label, use personal key 131 if (label.startsWith("Profile")) { 132 Text text = KeyList.PersonalKey.get(); 133 byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0)); 134 personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 135 return readLine(); 136 } else { 137 // TODO: resolve encryption label, what does this mean? 138 personalKey = resolveLabel(label); 139 return readLine(); 140 } 135 141 } 136 142 137 143 // decrypt line and return result 138 finalbyte[] toDecrypt = Base64.getDecoder().decode(line);139 finalbyte[] decrypted = DecryptSymmetric(toDecrypt, personalKey);144 byte[] toDecrypt = Base64.getDecoder().decode(line); 145 byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey); 140 146 if (decrypted == null) { 141 147 accessDenied = true; 142 148 return null; // access denied 143 149 } else { 144 finalString 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 {150 String decryptedLine = new String(decrypted); 151 if (decryptedLine.startsWith("Z")) { 152 return decryptedLine.trim(); 153 } else { 154 return decryptedLine; 155 } 156 } 157 } 152 158 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 } 159 private SecretKeySpec resolveLabel(String label) { 160 throw new NotImplementedException(); 161 } 162 } 163 164 // private class EncryptedProfileLineReader extends BufferedReader { 165 // 166 // public EncryptedProfileLineReader(final Reader in) { 167 // super(in); 168 // } 169 // 170 // @Override 171 // /** 172 // * Reads a line from an encrypted exp file that uses profile encryption (single key; personal key) 173 // * Returns that line to process, null if the currently logged in users personal key is not the appropriate one (access denied). 174 // */ 175 // public String readLine() throws IOException { 176 // // read encrypted line 177 // final String line = super.readLine(); 178 // 179 // if (line.isEmpty()) { return ""; } 180 // if (line.startsWith(ENCRYPTED_EXP_FLAG)) { 181 // // record/overwrite previous personal key then ignore this line by recursing 182 // final Text text = KeyList.PersonalKey.get(); 183 // final byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0)); 184 // personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 185 // return readLine(); 186 // } 187 // 188 // // decrypt line and return result 189 // final byte[] toDecrypt = Base64.getDecoder().decode(line); 190 // final byte[] decrypted = DecryptSymmetric(toDecrypt, personalKey); 191 // if (decrypted == null) { 192 // accessDenied = true; 193 // return null; // access denied 194 // } else { 195 // final String decryptedLine = new String(decrypted); 196 // if (decryptedLine.startsWith("Z")) { return decryptedLine.trim(); } 197 // else { return decryptedLine; } 198 // } 199 // } 200 // } 201 // 202 // private class EncryptedLineReader extends BufferedReader { 203 // 204 // public EncryptedLineReader(final Reader in) { 205 // super(in); 206 // } 207 // 208 // @Override 209 // /** 210 // * Reads a line from an encrypted exp file that uses (potentially multiple) labeled keys 211 // * Returns that line to process, null if the ... 212 // */ 213 // public String readLine() throws IOException { 214 // // read encrypted line 215 // final String line = super.readLine(); 216 // 217 // if (line.isEmpty()) { return ""; } 218 // if (line.startsWith(ENCRYPTED_EXP_FLAG)) { 219 // // resolve labels to secret keys 220 // final List<String> labels = Arrays.asList(line.split(" ")).stream().skip(1).collect(Collectors.toList()); 221 // final ThrowingFunction<String, SecretKey, Exception> worker = new ThrowingFunction<String, SecretKey, Exception>() { 222 // @Override 223 // public SecretKey applyThrows(final String label) throws Exception { 224 // return Authenticator.getInstance().getSecretKey(label, System.getProperty("password")); 225 // } 226 // }; 227 // multiKey = labels.stream().map(l -> { 228 // try { 229 // return worker.apply(l); 230 // } catch (final Exception e) { 231 // return null; 232 // } 233 // }).collect(Collectors.toList()); 234 // 235 // // confirm you have all the keys necessary for decryption 236 // if (multiKey.contains(null)) { 237 // return null; 238 // } 239 // 240 // // move onto the next line 241 // return readLine(); 242 // } 243 // 244 // // decrypt line and return result 245 // final byte[] toDecrypt = Base64.getDecoder().decode(line); 246 // byte[] decryptedBytes = null; 247 // for (final SecretKey key: multiKey) { 248 // decryptedBytes = DecryptSymmetric(toDecrypt, key); 249 // if (decryptedBytes == null) { return null; } 250 // } 251 // final String decrypted = new String(decryptedBytes); 252 // if (decrypted.startsWith("Z")) { return decrypted.trim(); } 253 // else { return decrypted; } 254 // } 255 // } 205 256 }
Note:
See TracChangeset
for help on using the changeset viewer.