1 | package org.expeditee.auth;
|
---|
2 |
|
---|
3 | import java.security.InvalidKeyException;
|
---|
4 | import java.security.NoSuchAlgorithmException;
|
---|
5 | import java.security.PrivateKey;
|
---|
6 | import java.security.PublicKey;
|
---|
7 | import java.sql.Connection;
|
---|
8 | import java.sql.DriverManager;
|
---|
9 | import java.sql.PreparedStatement;
|
---|
10 | import java.sql.SQLException;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Base64;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import javax.crypto.BadPaddingException;
|
---|
19 | import javax.crypto.Cipher;
|
---|
20 | import javax.crypto.IllegalBlockSizeException;
|
---|
21 | import javax.crypto.NoSuchPaddingException;
|
---|
22 |
|
---|
23 | import org.expeditee.gui.FrameIO;
|
---|
24 | import org.ngikm.cryptography.CryptographyConstants;
|
---|
25 |
|
---|
26 | public 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 | }
|
---|