4 import java.util.Arrays;
6 import javax.crypto.spec.*;
7 import java.security.SecureRandom;
10 * This class provides a communication API to the webserver. It also
11 * validates the HMACs on the slots and handles encryption.
12 * @author Brian Demsky <bdemsky@uci.edu>
24 static final int SALT_SIZE = 8;
29 * Empty Constructor needed for child class.
36 * Constructor for actual use. Takes in the url and password.
39 CloudComm(Table _table, String _baseurl, String _password) {
41 this.baseurl = _baseurl;
42 this.password = _password;
43 this.random = new SecureRandom();
47 * Generates Key from password.
50 private SecretKeySpec initKey() {
52 PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
53 SecretKey tmpkey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
54 return new SecretKeySpec(tmpkey.getEncoded(), "AES");
55 } catch (Exception e) {
57 throw new Error("Failed generating key.");
62 * Inits the HMAC generator.
65 private void initCrypt() {
67 SecretKeySpec key = initKey();
68 password = null; // drop password
69 mac = Mac.getInstance("HmacSHA256");
71 encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
72 encryptCipher.init(Cipher.ENCRYPT_MODE, key);
73 decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
74 decryptCipher.init(Cipher.DECRYPT_MODE, key);
75 } catch (Exception e) {
77 throw new Error("Failed To Initialize Ciphers");
82 * Builds the URL for the given request.
85 private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
86 String reqstring = isput ? "req=putslot" : "req=getslot";
87 String urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
89 urlstr += "&max=" + maxentries;
90 return new URL(urlstr);
93 public void setSalt() {
95 salt = new byte[SALT_SIZE];
96 random.nextBytes(salt);
97 URL url = new URL(baseurl + "?req=setsalt");
98 URLConnection con = url.openConnection();
99 HttpURLConnection http = (HttpURLConnection) con;
100 http.setRequestMethod("POST");
101 http.setFixedLengthStreamingMode(salt.length);
102 http.setDoOutput(true);
104 OutputStream os = http.getOutputStream();
106 int responsecode = http.getResponseCode();
107 if (responsecode != HttpURLConnection.HTTP_OK) {
108 // TODO: Remove this print
109 // System.out.println(responsecode);
110 throw new Error("Invalid response");
112 } catch (Exception e) {
114 throw new Error("Failed setting salt");
119 private void getSalt() throws Exception {
120 URL url = new URL(baseurl + "?req=getsalt");
121 URLConnection con = url.openConnection();
122 HttpURLConnection http = (HttpURLConnection) con;
123 http.setRequestMethod("POST");
126 InputStream is = http.getInputStream();
127 DataInputStream dis = new DataInputStream(is);
128 int salt_length = dis.readInt();
129 byte [] tmp = new byte[salt_length];
135 * API for putting a slot into the queue. Returns null on success.
136 * On failure, the server will send slots with newer sequence
140 Slot[] putSlot(Slot slot, int max) {
147 long sequencenumber = slot.getSequenceNumber();
148 byte[] bytes = slot.encode(mac);
149 bytes = encryptCipher.doFinal(bytes);
151 URL url = buildRequest(true, sequencenumber, max);
152 URLConnection con = url.openConnection();
153 HttpURLConnection http = (HttpURLConnection) con;
155 http.setRequestMethod("POST");
156 http.setFixedLengthStreamingMode(bytes.length);
157 http.setDoOutput(true);
160 OutputStream os = http.getOutputStream();
163 InputStream is = http.getInputStream();
164 DataInputStream dis = new DataInputStream(is);
165 byte[] resptype = new byte[7];
166 dis.readFully(resptype);
167 if (Arrays.equals(resptype, "getslot".getBytes()))
168 return processSlots(dis);
169 else if (Arrays.equals(resptype, "putslot".getBytes()))
172 throw new Error("Bad response to putslot");
173 } catch (Exception e) {
175 throw new Error("putSlot failed");
180 * Request the server to send all slots with the given
181 * sequencenumber or newer.
184 Slot[] getSlots(long sequencenumber) {
191 URL url = buildRequest(false, sequencenumber, 0);
192 URLConnection con = url.openConnection();
193 HttpURLConnection http = (HttpURLConnection) con;
194 http.setRequestMethod("POST");
196 InputStream is = http.getInputStream();
198 DataInputStream dis = new DataInputStream(is);
200 byte[] resptype = new byte[7];
201 dis.readFully(resptype);
202 if (!Arrays.equals(resptype, "getslot".getBytes()))
203 throw new Error("Bad Response: " + new String(resptype));
205 return processSlots(dis);
206 } catch (Exception e) {
208 throw new Error("getSlots failed");
213 * Method that actually handles building Slot objects from the
214 * server response. Shared by both putSlot and getSlots.
217 private Slot[] processSlots(DataInputStream dis) throws Exception {
218 int numberofslots = dis.readInt();
219 int[] sizesofslots = new int[numberofslots];
220 Slot[] slots = new Slot[numberofslots];
221 for (int i = 0; i < numberofslots; i++)
222 sizesofslots[i] = dis.readInt();
224 for (int i = 0; i < numberofslots; i++) {
225 byte[] data = new byte[sizesofslots[i]];
228 data = decryptCipher.doFinal(data);
230 slots[i] = Slot.decode(table, data, mac);