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;
25 static final int TIMEOUT_MILLIS = 100;
30 * Empty Constructor needed for child class.
36 * Constructor for actual use. Takes in the url and password.
38 CloudComm(Table _table, String _baseurl, String _password) {
40 this.baseurl = _baseurl;
41 this.password = _password;
42 this.random = new SecureRandom();
46 * Generates Key from password.
48 private SecretKeySpec initKey() {
50 PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
51 SecretKey tmpkey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
52 return new SecretKeySpec(tmpkey.getEncoded(), "AES");
53 } catch (Exception e) {
55 throw new Error("Failed generating key.");
60 * Inits the HMAC generator.
62 private void initCrypt() {
64 SecretKeySpec key = initKey();
65 password = null; // drop password
66 mac = Mac.getInstance("HmacSHA256");
68 encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
69 encryptCipher.init(Cipher.ENCRYPT_MODE, key);
70 decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
71 decryptCipher.init(Cipher.DECRYPT_MODE, key);
72 } catch (Exception e) {
74 throw new Error("Failed To Initialize Ciphers");
79 * Builds the URL for the given request.
81 private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
82 String reqstring = isput ? "req=putslot" : "req=getslot";
83 String urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
85 urlstr += "&max=" + maxentries;
86 return new URL(urlstr);
89 public void setSalt() throws ServerException {
91 byte[] saltTmp = new byte[SALT_SIZE];
92 random.nextBytes(saltTmp);
93 URL url = new URL(baseurl + "?req=setsalt");
94 URLConnection con = url.openConnection();
95 HttpURLConnection http = (HttpURLConnection) con;
96 http.setRequestMethod("POST");
97 http.setFixedLengthStreamingMode(saltTmp.length);
98 http.setDoOutput(true);
99 http.setConnectTimeout(TIMEOUT_MILLIS);
101 OutputStream os = http.getOutputStream();
103 int responsecode = http.getResponseCode();
104 if (responsecode != HttpURLConnection.HTTP_OK) {
105 // TODO: Remove this print
106 // System.out.println(responsecode);
107 throw new Error("Invalid response");
111 } catch (Exception e) {
113 throw new ServerException("Failed setting salt");
118 private void getSalt() throws Exception {
119 URL url = new URL(baseurl + "?req=getsalt");
120 URLConnection con = url.openConnection();
121 HttpURLConnection http = (HttpURLConnection) con;
122 http.setRequestMethod("POST");
125 InputStream is = http.getInputStream();
126 DataInputStream dis = new DataInputStream(is);
127 int salt_length = dis.readInt();
128 byte [] tmp = new byte[salt_length];
134 * API for putting a slot into the queue. Returns null on success.
135 * On failure, the server will send slots with newer sequence
139 Slot[] putSlot(Slot slot, int max) throws ServerException {
146 long sequencenumber = slot.getSequenceNumber();
147 byte[] bytes = slot.encode(mac);
148 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);
158 http.setConnectTimeout(TIMEOUT_MILLIS);
159 // http.setReadTimeout(TIMEOUT_MILLIS);
162 OutputStream os = http.getOutputStream();
167 InputStream is = http.getInputStream();
168 DataInputStream dis = new DataInputStream(is);
169 byte[] resptype = new byte[7];
170 dis.readFully(resptype);
172 if (Arrays.equals(resptype, "getslot".getBytes()))
173 return processSlots(dis);
174 else if (Arrays.equals(resptype, "putslot".getBytes()))
177 throw new Error("Bad response to putslot");
179 } catch (Exception e) {
180 throw new ServerException("putSlot failed");
186 * Request the server to send all slots with the given
187 * sequencenumber or newer.
189 Slot[] getSlots(long sequencenumber) throws ServerException {
196 URL url = buildRequest(false, sequencenumber, 0);
197 URLConnection con = url.openConnection();
198 HttpURLConnection http = (HttpURLConnection) con;
199 http.setRequestMethod("POST");
200 http.setConnectTimeout(TIMEOUT_MILLIS);
201 // http.setReadTimeout(TIMEOUT_MILLIS);
203 InputStream is = http.getInputStream();
204 DataInputStream dis = new DataInputStream(is);
206 int responsecode = http.getResponseCode();
207 if (responsecode != HttpURLConnection.HTTP_OK) {
208 // TODO: Remove this print
209 // System.out.println("Code: " + responsecode);
210 throw new ServerException("getSlots failed");
213 byte[] resptype = new byte[7];
214 dis.readFully(resptype);
215 if (!Arrays.equals(resptype, "getslot".getBytes()))
216 throw new Error("Bad Response: " + new String(resptype));
218 return processSlots(dis);
219 } catch (Exception e) {
220 // e.printStackTrace();
221 throw new ServerException("getSlots failed");
226 * Method that actually handles building Slot objects from the
227 * server response. Shared by both putSlot and getSlots.
229 private Slot[] processSlots(DataInputStream dis) throws Exception {
230 int numberofslots = dis.readInt();
231 int[] sizesofslots = new int[numberofslots];
232 Slot[] slots = new Slot[numberofslots];
233 for (int i = 0; i < numberofslots; i++)
234 sizesofslots[i] = dis.readInt();
236 for (int i = 0; i < numberofslots; i++) {
237 byte[] data = new byte[sizesofslots[i]];
240 data = decryptCipher.doFinal(data);
242 slots[i] = Slot.decode(table, data, mac);