1 | package org.expeditee.auth;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.security.InvalidKeyException;
|
---|
5 | import java.security.NoSuchAlgorithmException;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Base64;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.crypto.BadPaddingException;
|
---|
12 | import javax.crypto.Cipher;
|
---|
13 | import javax.crypto.IllegalBlockSizeException;
|
---|
14 | import javax.crypto.NoSuchPaddingException;
|
---|
15 | import javax.crypto.SecretKey;
|
---|
16 | import javax.crypto.spec.SecretKeySpec;
|
---|
17 |
|
---|
18 | import org.expeditee.encryption.CryptographyConstants;
|
---|
19 | import org.expeditee.gui.Frame;
|
---|
20 | import org.expeditee.gui.FrameIO;
|
---|
21 | import org.expeditee.gui.MessageBay;
|
---|
22 | import org.expeditee.io.ExpWriter;
|
---|
23 | import org.expeditee.items.Text;
|
---|
24 | import org.expeditee.settings.UserSettings;
|
---|
25 | import org.expeditee.settings.identity.secrets.KeyList;
|
---|
26 |
|
---|
27 | public class EncryptedExpWriter extends ExpWriter implements CryptographyConstants {
|
---|
28 | private SecretKey key;
|
---|
29 | private String label;
|
---|
30 | private static final String nl = "\n";
|
---|
31 |
|
---|
32 | public EncryptedExpWriter(String encryptionLabel) throws IOException {
|
---|
33 | if (encryptionLabel.compareTo("Profile") == 0) {
|
---|
34 | // obtain personal key
|
---|
35 | Text text = KeyList.PersonalKey.get();
|
---|
36 | List<String> data = text.getData();
|
---|
37 | if (data != null && !data.isEmpty()) {
|
---|
38 | byte[] keyBytes = Base64.getDecoder().decode(data.get(0));
|
---|
39 | key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
40 | }
|
---|
41 | label = "Profile";
|
---|
42 | } else {
|
---|
43 | byte[] keyBytes = resolveKeyFromLabel(encryptionLabel, _framename);
|
---|
44 | key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
45 | label = encryptionLabel;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | protected void preOutputFrame() {
|
---|
51 | try {
|
---|
52 | String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + label + nl;
|
---|
53 | _writer.write(line);
|
---|
54 | _stringWriter.append(line);
|
---|
55 | } catch (final IOException e) {
|
---|
56 | e.printStackTrace();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | protected void writeLine(String line) throws IOException {
|
---|
62 | // do not write empty lines
|
---|
63 | if (line == null) { return; }
|
---|
64 |
|
---|
65 | // prepare line to write out
|
---|
66 | byte[] encrypted = EncryptSymmetric(line.getBytes(), key);
|
---|
67 | String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
|
---|
68 |
|
---|
69 | // output
|
---|
70 | _writer.write(toWrite);
|
---|
71 | _stringWriter.append(toWrite);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected static byte[] resolveKeyFromLabel(String label, String framename) {
|
---|
75 | String credentialsFrameName = UserSettings.ProfileName.get() + AuthenticatorBrowser.CREDENTIALS_FRAME;
|
---|
76 | Frame credentialsFrame = FrameIO.LoadFrame(credentialsFrameName);
|
---|
77 | Collection<Text> textItems = credentialsFrame.getTextItems();
|
---|
78 | textItems.removeIf(t -> !t.getText().equals("Secrets"));
|
---|
79 | textItems.removeIf(t -> !t.hasLink());
|
---|
80 | if (textItems.isEmpty()) {
|
---|
81 | MessageBay.errorMessage("Unable to find label " + label + " to encrypt frame " + framename + ".");
|
---|
82 | return null;
|
---|
83 | } else {
|
---|
84 | Text linkToSecretsFrame = textItems.iterator().next();
|
---|
85 | Frame secretsFrame = FrameIO.LoadFrame(linkToSecretsFrame.getParent().getFramesetName() + linkToSecretsFrame.getLink());
|
---|
86 | Collection<Text> labels = secretsFrame.getTextItems();
|
---|
87 | labels.removeIf(lbl -> !lbl.getText().equals(label));
|
---|
88 | labels.removeIf(lbl -> lbl.getData() == null || lbl.getData().size() == 0);
|
---|
89 | if (labels.isEmpty()) {
|
---|
90 | MessageBay.errorMessage("Unable to find label " + label + " to encrypt frame " + framename + ".");
|
---|
91 | return null;
|
---|
92 | }
|
---|
93 |
|
---|
94 | Text labelItem = labels.iterator().next();
|
---|
95 | String data = labelItem.getData().get(0);
|
---|
96 | if (data.contains("{")) {
|
---|
97 | MessageBay.errorMessage("You only have a fraction of the required key to access " + framename + ".");
|
---|
98 | return null;
|
---|
99 | } else {
|
---|
100 | try {
|
---|
101 | return Base64.getDecoder().decode(data);
|
---|
102 | } catch (IllegalArgumentException e) {
|
---|
103 | MessageBay.errorMessage("Unable to create key out of data stored in label " + label + ".");
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | private static byte[] EncryptSymmetric(byte[] toEncrypt, SecretKey key) {
|
---|
111 | try {
|
---|
112 | Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
|
---|
113 | cipher.init(Cipher.ENCRYPT_MODE, key);
|
---|
114 | //could use modulus
|
---|
115 | int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
|
---|
116 | byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
|
---|
117 | byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
|
---|
118 | return result;
|
---|
119 | } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
---|
120 | | IllegalBlockSizeException | BadPaddingException e) {
|
---|
121 | e.printStackTrace();
|
---|
122 | return null;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|