Changeset 1277
- Timestamp:
- 04/02/19 15:22:06 (5 years ago)
- Location:
- trunk/src/org/expeditee
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/auth/Actions.java
r1271 r1277 21 21 import java.security.spec.InvalidKeySpecException; 22 22 import java.security.spec.PKCS8EncodedKeySpec; 23 import java.sql.Connection;24 import java.sql.DriverManager;25 23 import java.sql.SQLException; 26 import java. sql.Statement;24 import java.text.ParseException; 27 25 import java.util.Base64; 28 26 import java.util.Collection; … … 79 77 options.put("Neat", "Beep"); 80 78 MailEntry mail = new MailEntry(time, sender, colleagueName, topic, message, options); 81 PublicKey publicKey = Authenticator.getInstance().getPublicKey(colleagueName); 82 Path outbox = Paths.get(FrameIO.PROFILE_PATH).resolve(sender).resolve(sender + "-credentials"); 83 Mail.sendMail(mail, publicKey, outbox); 79 Mail.sendMail(mail, colleagueName); 84 80 } 85 81 private static String userbackup = "authadmin"; … … 99 95 * @throws FileNotFoundException 100 96 * @throws KeyStoreException 101 */ 102 public static void MailMode() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, SQLException, IOException { 97 * @throws ParseException 98 */ 99 public static void MailMode() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, SQLException, IOException, ParseException { 103 100 if (!DisplayController.isMailMode()) { 104 101 Mail.clear(); … … 133 130 * @throws InvalidKeySpecException 134 131 * @throws InvalidKeyException 132 * @throws ParseException 135 133 * @throws Exception 136 134 */ 137 public static void AuthCreateAccount() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, ClassNotFoundException, InvalidFramesetNameException, ExistingFramesetException, IOException, SQLException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {135 public static void AuthCreateAccount() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, ClassNotFoundException, InvalidFramesetNameException, ExistingFramesetException, IOException, SQLException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ParseException { 138 136 Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems(); 139 137 Optional<Map<AuthenticationTag, String>> userdata = … … 192 190 login(userdata.get()); 193 191 Authenticator.Authenticated = true; 194 } 195 else { 192 } else { 196 193 MessageBay.errorMessage(Constants.ERROR_INSUFFICIENT_INFORMATION_PROVIDED); 197 194 } … … 355 352 */ 356 353 private static void createAccount(Map<AuthenticationTag, String> userdata) throws InvalidFramesetNameException, ExistingFramesetException, 357 KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, ClassNotFoundException, IOException, SQLException { 358 354 KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, ClassNotFoundException, IOException, SQLException { 359 355 // Track progress 360 356 String message = "Creating new user account..."; 361 //Progress progress = MessageBay.displayProgress(message);362 357 363 358 // Extract user details … … 469 464 Path destinationFile = destinationDirectory.resolve(Authenticator.CREDENTIALS_FRAME + ExpReader.EXTENTION); 470 465 FrameIO.migrateFrame(credentialsFrame, destinationFile); 471 // outbox472 Connection c = DriverManager.getConnection("jdbc:sqlite:" + credentialsDir.getAbsolutePath() + File.separator + "expmail.db");473 Statement createTable = c.createStatement();474 String sql = "CREATE TABLE EXPMAIL (" +475 "TIME TEXT NOT NULL, " +476 "SND TEXT NOT NULL, " +477 "REC TEXT NOT NULL, " +478 "MSG TEXT NOT NULL, " +479 "MSG2 TEXT NOT NULL, " +480 "OPTS ARRAY NOT NULL, " +481 "OPTSVAL ARRAY NOT NULL)";482 createTable.executeUpdate(sql);483 createTable.close();484 c.close();485 466 486 467 System.out.println(message + "Creating Individual Space."); … … 492 473 contactsDir.mkdir(); 493 474 475 System.out.println(message + "Creating Space For Dead Drops"); 476 477 File deadDropsDir = new File(personalResources.resolve("deaddrops").toAbsolutePath().toString()); 478 deadDropsDir.mkdir(); 479 494 480 System.err.println("**** Hardwired call in Apollo's AuthioPathManager"); 495 481 AudioPathManager.activateAndScanAudioDir(); // **** … … 497 483 System.out.println(message + "Done."); 498 484 } 499 500 501 485 502 486 /* 503 487 * Function used to authenticate as a specified user (via function arguments). 504 488 */ 505 private static void login(Map<AuthenticationTag, String> userdata) throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, ClassNotFoundException, IOException, SQLException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {489 private static void login(Map<AuthenticationTag, String> userdata) throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, ClassNotFoundException, IOException, SQLException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ParseException { 506 490 String username = userdata.get(AuthenticationTag.Username); 507 491 String password = userdata.get(AuthenticationTag.Password); … … 539 523 personalKeyText.setData(Base64.getEncoder().encodeToString(personalKey.getEncoded())); 540 524 KeyList.PersonalKey.setSetting(personalKeyText); 525 UserSettings.UserName.set(username); 541 526 oneFrame = FrameIO.LoadProfile(username); 542 527 for (int i = 1; i <= FrameIO.getLastNumber(username); i++) { … … 608 593 options.put("Clear Preview Colleague Nominations", "AuthClearPWColleaguesNominated"); 609 594 MailEntry mail = new MailEntry(time, sender, colleagueName, message, message2, options); 610 Mail.sendMail(mail, Authenticator.getInstance().getPublicKey(colleagueName), Paths.get(FrameIO.PROFILE_PATH).resolve(sender).resolve(sender + "-credentials"));595 Mail.sendMail(mail, colleagueName); 611 596 } 612 597 … … 631 616 arguments.put("I wish to excuse myself from this responsibility.", "AuthDenyPasswordColleagueRelationship " + sender); 632 617 MailEntry mail = new MailEntry(time, sender, colleagueOne, topic, message, arguments); 633 Path outbox = Paths.get(FrameIO.PROFILE_PATH).resolve(sender).resolve(sender + "-credentials"); 634 Mail.sendMail(mail, colleagueOneKey, outbox); 618 Mail.sendMail(mail, colleagueOne); 635 619 mail = new MailEntry(time, sender, colleagueTwo, topic, message, arguments); 636 Mail.sendMail(mail, colleagueTwo Key, outbox);620 Mail.sendMail(mail, colleagueTwo); 637 621 Authenticator.getInstance().markRequestedColleagues(UserSettings.UserName.get()); 638 622 return true; -
trunk/src/org/expeditee/auth/Authenticator.java
r1270 r1277 2 2 3 3 import java.io.File; 4 import java.io.FileFilter; 4 5 import java.io.FileInputStream; 5 6 import java.io.FileNotFoundException; 6 7 import java.io.FileOutputStream; 8 import java.io.FileWriter; 7 9 import java.io.IOException; 8 10 import java.io.InputStream; … … 25 27 import java.sql.ResultSet; 26 28 import java.sql.SQLException; 29 import java.text.ParseException; 30 import java.text.SimpleDateFormat; 31 import java.util.ArrayList; 27 32 import java.util.Arrays; 28 33 import java.util.Base64; 29 34 import java.util.Collection; 35 import java.util.Date; 30 36 import java.util.HashMap; 37 import java.util.List; 31 38 import java.util.Map; 32 39 import java.util.Scanner; … … 59 66 import org.expeditee.settings.UserSettings; 60 67 import org.expeditee.settings.identity.secrets.KeyList; 68 import org.expeditee.stats.Formatter; 61 69 import org.ngikm.cryptography.CryptographyConstants; 62 70 … … 172 180 } 173 181 174 final void loadMailFrom Directory(Path contactDir) throws SQLException {182 final void loadMailFromFile(Path dbFile) throws SQLException { 175 183 // Load in all mail. 176 Connection c = DriverManager.getConnection("jdbc:sqlite:" + contactDir.resolve("expmail.db"));184 Connection c = DriverManager.getConnection("jdbc:sqlite:" + dbFile.toAbsolutePath().toString()); 177 185 String sql = "SELECT * FROM EXPMAIL"; 178 186 PreparedStatement query = c.prepareStatement(sql); … … 209 217 } 210 218 211 final void loadMailDatabase() throws SQLException { 212 Path contactsPath = Paths.get(FrameIO.CONTACTS_PATH); 213 File[] contacts = contactsPath.toFile().listFiles(); 214 for (int i = 0; i < contacts.length; i++) { 215 if (contacts[i].isDirectory()) { 216 Path contact = Paths.get(contacts[i].getAbsolutePath()); 217 loadMailFromDirectory(contact); 218 } 219 } 220 } 221 219 final void loadMailDatabase() throws SQLException, FileNotFoundException, ParseException { 220 Path deadDropPath = Paths.get(FrameIO.DEAD_DROPS_PATH); 221 for (File connectionDir: deadDropPath.toFile().listFiles()) { 222 if (connectionDir.isDirectory()) { 223 Path deaddropforcontactPath = Paths.get(connectionDir.getAbsolutePath()); 224 Path dbFile = deaddropforcontactPath.resolve(UserSettings.UserName.get() + ".db"); 225 if (dbFile.toFile().exists()) { 226 loadMailFromFile(dbFile); 227 } 228 Path timestamp = deaddropforcontactPath.resolve(UserSettings.UserName.get() + ".last-accessed"); 229 try(FileWriter out = new FileWriter(timestamp.toFile())) { 230 out.write(Formatter.getDateTime() + System.getProperty("line.separator")); 231 } catch (IOException e) { 232 e.printStackTrace(); 233 } 234 clearOldMailFromDatabase(deaddropforcontactPath); 235 } 236 } 237 } 238 239 private void clearOldMailFromDatabase(Path directory) throws FileNotFoundException, ParseException, SQLException { 240 File[] files = directory.toFile().listFiles(new FileFilter() { 241 @Override 242 public boolean accept(File file) { 243 return !file.getName().startsWith(UserSettings.UserName.get()); 244 } 245 }); 246 247 File dbFile = null; 248 File lastAccessedFile = null; 249 for (File file: files) { 250 if (file.getName().endsWith(".db")) { 251 dbFile = file; 252 } else { 253 lastAccessedFile = file; 254 } 255 } 256 257 if (dbFile == null || lastAccessedFile == null) { 258 System.err.println("Failed to clear old messages from database file."); 259 return; // Not the end of the world if we cannot clear out old messages. 260 } 261 262 SimpleDateFormat format = new SimpleDateFormat("ddMMMyyyy[HH:mm]"); 263 Date timestamp = null; 264 try(Scanner in = new Scanner(lastAccessedFile)) { 265 timestamp = format.parse(in.nextLine()); 266 } 267 268 Connection c = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath()); 269 String sql = "SELECT * FROM EXPMAIL"; 270 PreparedStatement query = c.prepareStatement(sql); 271 ResultSet allMail = query.executeQuery(); 272 List<String> oldTimestamps = new ArrayList<String>(); 273 274 while (allMail.next()) { 275 String time = allMail.getString("time"); 276 Date messageTimestamp = format.parse(time); 277 if (timestamp.after(messageTimestamp)) { 278 oldTimestamps.add(time); 279 } 280 } 281 282 System.err.println("Found " + oldTimestamps.size() + " messages to delete."); 283 // TODO delete the messages from database. 284 } 285 222 286 final SecretKey getSecretKey(final String label, final String password) throws NoSuchAlgorithmException, KeyStoreException { 223 287 224 288 char[] password_ca = password.toCharArray(); 225 //final KeyStore.ProtectionParameter entryPassword = new KeyStore.PasswordProtection(password_ca);226 289 227 290 SecretKey secret_key; 228 291 try { 229 //SecretKeyEntry entry = (SecretKeyEntry) keyStore.getEntry(label, entryPassword);230 //secret_key = entry.getSecretKey();231 292 secret_key = (SecretKey) keyStore.getKey(label, password_ca); 232 293 } catch (final UnrecoverableEntryException e) { -
trunk/src/org/expeditee/auth/Mail.java
r1243 r1277 1 1 package org.expeditee.auth; 2 2 3 import java.io.File; 4 import java.io.IOException; 3 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 4 7 import java.security.InvalidKeyException; 8 import java.security.KeyStoreException; 5 9 import java.security.NoSuchAlgorithmException; 6 10 import java.security.PrivateKey; 7 11 import java.security.PublicKey; 12 import java.security.cert.CertificateException; 13 import java.security.spec.InvalidKeySpecException; 8 14 import java.sql.Connection; 9 15 import java.sql.DriverManager; 10 16 import java.sql.PreparedStatement; 11 17 import java.sql.SQLException; 18 import java.sql.Statement; 12 19 import java.util.ArrayList; 13 20 import java.util.Arrays; … … 22 29 import javax.crypto.NoSuchPaddingException; 23 30 31 import org.expeditee.gui.FrameIO; 24 32 import org.ngikm.cryptography.CryptographyConstants; 25 33 … … 39 47 } 40 48 41 public static void sendMail(MailEntry mail, PublicKey key, Path outbox) { 49 public static void sendMail(MailEntry mail, String colleagueName) { 50 // Ensure dead drop area is set up. 51 Path databaseFileDirPath = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(colleagueName); 52 Path databaseFilePath = databaseFileDirPath.resolve(colleagueName + ".db"); 53 File databaseFile = databaseFilePath.toFile(); 54 if (!databaseFile.exists()) { 55 databaseFileDirPath.toFile().mkdirs(); 56 String sql = 57 "CREATE TABLE EXPMAIL (" + 58 "TIME TEXT NOT NULL, " + 59 "SND TEXT NOT NULL, " + 60 "REC TEXT NOT NULL, " + 61 "MSG TEXT NOT NULL, " + 62 "MSG2 TEXT NOT NULL, " + 63 "OPTS ARRAY NOT NULL, " + 64 "OPTSVAL ARRAY NOT NULL)"; 65 try { 66 Connection c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath()); 67 Statement createTable = c.createStatement(); 68 createTable.executeUpdate(sql); 69 createTable.close(); 70 c.close(); 71 } catch (SQLException e) { 72 System.err.println("Error while creating database file."); 73 e.printStackTrace(); 74 } 75 } 76 77 // Obtain public key 78 PublicKey publicKey = null; 79 try { 80 publicKey = Authenticator.getInstance().getPublicKey(colleagueName); 81 } catch (InvalidKeySpecException | NoSuchAlgorithmException | KeyStoreException | CertificateException 82 | ClassNotFoundException | IOException | SQLException e) { 83 System.err.println("Error while sending message. Unable to obtain public key for colleague " + 84 colleagueName + ". Exception message: " + e.getMessage()); 85 return; 86 } 87 88 // Check we got public key 89 if (publicKey == null) { 90 System.err.println("Error while sending message. Unable to obtain public key for colleague. Have you exchanged contact details?"); 91 return; 92 } 93 94 // Send message 95 sendMail(mail, publicKey, databaseFilePath); 96 } 97 98 private static void sendMail(MailEntry mail, PublicKey key, Path databaseFile) { 42 99 try { 43 100 Cipher cipher = Cipher.getInstance(AsymmetricAlgorithm + AsymmetricAlgorithmParameters); 44 101 45 102 // encrypt the necessary parts of the message 46 cipher.init(Cipher.ENCRYPT_MODE, key);47 String time = Base64.getEncoder().encodeToString(cipher.doFinal(mail.timestamp.getBytes()));103 //cipher.init(Cipher.ENCRYPT_MODE, key); 104 //String time = Base64.getEncoder().encodeToString(cipher.doFinal(mail.timestamp.getBytes())); 48 105 cipher.init(Cipher.ENCRYPT_MODE, key); 49 106 String sender = Base64.getEncoder().encodeToString(cipher.doFinal(mail.sender.getBytes())); … … 65 122 66 123 // write to mail database 67 Connection c = DriverManager.getConnection("jdbc:sqlite:" + outbox.resolve("expmail.db"));124 Connection c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile); 68 125 String sql = "INSERT INTO EXPMAIL (TIME,SND,REC,MSG,MSG2,OPTS,OPTSVAL) VALUES (?, ?, ?, ?, ?, ?, ?);"; 69 126 PreparedStatement statement = c.prepareStatement(sql); 70 statement.setString(1, time);127 statement.setString(1, mail.timestamp); 71 128 statement.setString(2, sender); 72 129 statement.setString(3, rec); … … 110 167 if (receiverDecrypted.compareToIgnoreCase(name) == 0) { 111 168 c.init(Cipher.DECRYPT_MODE, key); 112 String timestamp = new String(c.doFinal(Base64.getDecoder().decode(mail.timestamp)));113 c.init(Cipher.DECRYPT_MODE, key);114 169 String sender = new String(c.doFinal(Base64.getDecoder().decode(mail.sender))); 115 170 c.init(Cipher.DECRYPT_MODE, key); … … 128 183 129 184 //String arguments = new String(c.doFinal(Base64.getDecoder().decode(mail.args))); 130 filtered.add(new MailEntry( timestamp, sender, receiverDecrypted, message, message2, options));185 filtered.add(new MailEntry(mail.timestamp, sender, receiverDecrypted, message, message2, options)); 131 186 } 132 187 } -
trunk/src/org/expeditee/gui/FrameIO.java
r1274 r1277 116 116 public static String HELP_PRIVATE_PATH; 117 117 public static String HELP_PATH; 118 public static String DEAD_DROPS_PATH; 118 119 119 120 // Paths that appear to be unused. … … 139 140 PUBLIC_PATH = PARENT_FOLDER + "public" + File.separator; 140 141 TRASH_PATH = PARENT_FOLDER + "trash" + File.separator; 141 //MESSAGES_PATH = PARENT_FOLDER + "messages" + File.separator;142 142 HELP_PATH = PARENT_FOLDER + "documentation" + File.separator; 143 143 PROFILE_PATH = PARENT_FOLDER + "profiles" + File.separator; … … 162 162 AUDIO_PATH = PARENT_FOLDER + "audio" + File.separator; 163 163 FRAME_PATH = PARENT_FOLDER + "framesets" + File.separator; 164 DEAD_DROPS_PATH = PARENT_FOLDER + "deaddrops" + File.separator; 164 165 } 165 166 … … 199 200 MESSAGES_PATH = resourcesPrivateIndividualPath + "messages" + File.separator; 200 201 MAIL_PATH = resourcesPrivateIndividualPath + "mail" + File.separator; 202 DEAD_DROPS_PATH = resourcesPrivateIndividualPath + "deaddrops" + File.separator; 201 203 } 202 204 -
trunk/src/org/expeditee/settings/UserSettings.java
r1272 r1277 259 259 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.PUBLIC_PATH); 260 260 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.HELP_PRIVATE_PATH); 261 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.MAIL_PATH); 261 262 FolderSettings.FrameDirs.setDefault(FolderSettings.FrameDirs.get()); 262 263 … … 271 272 FolderSettings.AudioDirs.addAbsoluteDir(FrameIO.AUDIO_PATH); 272 273 FolderSettings.AudioDirs.setDefault(FolderSettings.AudioDirs.get()); 273 274 274 } else { 275 275 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.FRAME_PATH);
Note:
See TracChangeset
for help on using the changeset viewer.