source: trunk/src/org/expeditee/auth/Mail.java@ 1203

Last change on this file since 1203 was 1202, checked in by bln4, 5 years ago

Initial commit of functionality concerning multiuser login, further to come.

Actions.java -> Actions that allow users to authenticate and secure their accounts.
AuthenticationTag.java -> Enum like structure for text fields associated with authentication.
Authenticator.java -> Startup functionality for when Expeditee is run in authentication mode.
EncryptedExpReader.java -> Reads exp files previously encrypted with EncryptedExpWriter (not currently used) and EncryptedProfileExpWriter
Mail.java -> Functions for transforming database stored messages into datastructures used to display those messages to the MailBay.

File size: 5.9 KB
Line 
1package org.expeditee.auth;
2
3import java.security.InvalidKeyException;
4import java.security.NoSuchAlgorithmException;
5import java.security.PrivateKey;
6import java.security.PublicKey;
7import java.sql.Connection;
8import java.sql.DriverManager;
9import java.sql.PreparedStatement;
10import java.sql.SQLException;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Base64;
14import java.util.HashMap;
15import java.util.List;
16import java.util.Map;
17
18import javax.crypto.BadPaddingException;
19import javax.crypto.Cipher;
20import javax.crypto.IllegalBlockSizeException;
21import javax.crypto.NoSuchPaddingException;
22
23import org.expeditee.gui.FrameIO;
24import org.ngikm.cryptography.CryptographyConstants;
25
26public class Mail implements CryptographyConstants {
27
28 private static List<MailEntry> messages = new ArrayList<MailEntry>();
29
30 /**
31 * Add a piece of mail, used during initialisation.
32 */
33 public static void addEntry(MailEntry mail) {
34 messages.add(mail);
35 }
36
37 public static void clear() {
38 messages.clear();
39 }
40
41 public static void sendMail(MailEntry mail, PublicKey key) {
42 try {
43 Cipher cipher = Cipher.getInstance(AsymmetricAlgorithm + AsymmetricAlgorithmParameters);
44
45 // encrypt the necessary parts of the message
46 cipher.init(Cipher.ENCRYPT_MODE, key);
47 String sender = Base64.getEncoder().encodeToString(cipher.doFinal(mail.sender.getBytes()));
48 cipher.init(Cipher.ENCRYPT_MODE, key);
49 String rec = Base64.getEncoder().encodeToString(cipher.doFinal(mail.receiver.getBytes()));
50 cipher.init(Cipher.ENCRYPT_MODE, key);
51 String message = Base64.getEncoder().encodeToString(cipher.doFinal(mail.message.getBytes()));
52 cipher.init(Cipher.ENCRYPT_MODE, key);
53 String message2 = Base64.getEncoder().encodeToString(cipher.doFinal(mail.message2.getBytes()));
54
55 Map<String, String> options = new HashMap<String, String>();
56 for (String label: mail.options.keySet()) {
57 cipher.init(Cipher.ENCRYPT_MODE, key);
58 String labelEncrypted = Base64.getEncoder().encodeToString(cipher.doFinal(label.getBytes()));
59 cipher.init(Cipher.ENCRYPT_MODE, key);
60 String actionNameEncrypted = Base64.getEncoder().encodeToString(cipher.doFinal(mail.options.get(label).getBytes()));
61 options.put(labelEncrypted, actionNameEncrypted);
62 }
63
64 // write to mail database
65 Connection c = DriverManager.getConnection("jdbc:sqlite:" + FrameIO.PARENT_FOLDER + "/expmail.db");
66 String sql = "INSERT INTO EXPMAIL (SND,REC,MSG,MSG2,OPTS,OPTSVAL) VALUES (?, ?, ?, ?, ?, ?);";
67 PreparedStatement statement = c.prepareStatement(sql);
68 statement.setString(1, sender);
69 statement.setString(2, rec);
70 statement.setString(3, message);
71 statement.setString(4, message2);
72 String opts = Arrays.toString(options.keySet().toArray());
73 statement.setString(5, opts);
74 String optsval = Arrays.toString(options.values().toArray());
75 statement.setString(6, optsval);
76 statement.execute();
77 statement.close();
78 c.close();
79 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | SQLException e) {
80 e.printStackTrace();
81 }
82 }
83
84 /**
85 * Gets the mail messages that the specified user is able to read.
86 * The caller supplies their username and private key.
87 * If the private key can decrypt a message, then it was encrypted using the users public key, and is therefore for them.
88 */
89 public static List<MailEntry> getEntries(String name, PrivateKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
90 List<MailEntry> filtered = new ArrayList<MailEntry>();
91
92 for (MailEntry mail: messages) {
93 // confirm this is a message for the requester of entries
94 String receiver = mail.receiver;
95 byte[] receiverBytes = Base64.getDecoder().decode(receiver);
96 String receiverDecrypted = null;
97 Cipher c = Cipher.getInstance(AsymmetricAlgorithm + AsymmetricAlgorithmParameters);
98 try {
99 c.init(Cipher.DECRYPT_MODE, key);
100 receiverDecrypted = new String(c.doFinal(receiverBytes));
101 } catch (InvalidKeyException | BadPaddingException e) {
102 // this is not a message for 'us'
103 continue;
104 }
105
106 // add an unencrypted version of the message to the return list
107 if (receiverDecrypted.compareTo(name) == 0) {
108 c.init(Cipher.DECRYPT_MODE, key);
109 String sender = new String(c.doFinal(Base64.getDecoder().decode(mail.sender)));
110 c.init(Cipher.DECRYPT_MODE, key);
111 String message = new String(c.doFinal(Base64.getDecoder().decode(mail.message)));
112 c.init(Cipher.DECRYPT_MODE, key);
113 String message2 = new String(c.doFinal(Base64.getDecoder().decode(mail.message2)));
114
115 Map<String, String> options = new HashMap<String, String>(); //mail.options;
116 for (String label: mail.options.keySet()) {
117 c.init(Cipher.DECRYPT_MODE, key);
118 String labelDecrypted = new String(c.doFinal(Base64.getDecoder().decode(label)));
119 c.init(Cipher.DECRYPT_MODE, key);
120 String actionNameDecrypted = new String(c.doFinal(Base64.getDecoder().decode(mail.options.get(label))));
121 options.put(labelDecrypted, actionNameDecrypted);
122 }
123
124 //String arguments = new String(c.doFinal(Base64.getDecoder().decode(mail.args)));
125 filtered.add(new MailEntry(sender, receiverDecrypted, message, message2, options));
126 }
127 }
128
129 return filtered;
130 }
131
132 /**
133 * Describes a piece of mail, either encrypted or decrypted.
134 */
135 public static class MailEntry {
136 public String sender;
137 public String receiver;
138 public String message;
139 public String message2;
140 public Map<String, String> options;
141
142 public MailEntry(String sender, String rec, String message, String message2, Map<String, String> options) {
143 this.sender = sender;
144 this.receiver = rec;
145 this.message = message;
146 this.message2 = message2;
147 this.options = options;
148 }
149 }
150}
Note: See TracBrowser for help on using the repository browser.