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>
25 static final int SALT_SIZE = 8;
26 static final int TIMEOUT_MILLIS = 100;
31 * Empty Constructor needed for child class.
37 * Constructor for actual use. Takes in the url and password.
39 CloudComm(Table _table, String _hostname, String _baseurl, String _password) {
41 this.hostname = _hostname;
42 this.baseurl = _baseurl;
43 this.password = _password;
44 this.random = new SecureRandom();
48 * 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.
64 private void initCrypt() {
66 SecretKeySpec key = initKey();
67 password = null; // drop password
68 mac = Mac.getInstance("HmacSHA256");
70 encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
71 encryptCipher.init(Cipher.ENCRYPT_MODE, key);
72 decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
73 decryptCipher.init(Cipher.DECRYPT_MODE, key);
74 } catch (Exception e) {
76 throw new Error("Failed To Initialize Ciphers");
81 * Builds the URL for the given request.
83 private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
84 String reqstring = isput ? "req=putslot" : "req=getslot";
85 String urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
87 urlstr += "&max=" + maxentries;
88 return new URL(urlstr);
91 public void setSalt() throws ServerException {
93 byte[] saltTmp = new byte[SALT_SIZE];
94 random.nextBytes(saltTmp);
95 URL url = new URL(baseurl + "?req=setsalt");
96 URLConnection con = url.openConnection();
97 HttpURLConnection http = (HttpURLConnection) con;
98 http.setRequestMethod("POST");
99 http.setFixedLengthStreamingMode(saltTmp.length);
100 http.setDoOutput(true);
101 http.setConnectTimeout(TIMEOUT_MILLIS);
103 OutputStream os = http.getOutputStream();
105 int responsecode = http.getResponseCode();
106 if (responsecode != HttpURLConnection.HTTP_OK) {
107 // TODO: Remove this print
108 // System.out.println(responsecode);
109 throw new Error("Invalid response");
113 } catch (Exception e) {
114 throw new ServerException("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) throws ServerException {
147 long sequencenumber = slot.getSequenceNumber();
148 byte[] bytes = slot.encode(mac);
149 bytes = encryptCipher.doFinal(bytes);
152 URL url = buildRequest(true, sequencenumber, max);
153 URLConnection con = url.openConnection();
154 HttpURLConnection http = (HttpURLConnection) con;
156 http.setRequestMethod("POST");
157 http.setFixedLengthStreamingMode(bytes.length);
158 http.setDoOutput(true);
159 http.setConnectTimeout(TIMEOUT_MILLIS);
160 // http.setReadTimeout(TIMEOUT_MILLIS);
163 OutputStream os = http.getOutputStream();
168 InputStream is = http.getInputStream();
169 DataInputStream dis = new DataInputStream(is);
170 byte[] resptype = new byte[7];
171 dis.readFully(resptype);
173 if (Arrays.equals(resptype, "getslot".getBytes()))
174 return processSlots(dis);
175 else if (Arrays.equals(resptype, "putslot".getBytes()))
178 throw new Error("Bad response to putslot");
180 } catch (Exception e) {
181 throw new ServerException("putSlot failed");
187 * Request the server to send all slots with the given
188 * sequencenumber or newer.
190 Slot[] getSlots(long sequencenumber) throws ServerException {
197 URL url = buildRequest(false, sequencenumber, 0);
198 URLConnection con = url.openConnection();
199 HttpURLConnection http = (HttpURLConnection) con;
200 http.setRequestMethod("POST");
201 http.setConnectTimeout(TIMEOUT_MILLIS);
202 // http.setReadTimeout(TIMEOUT_MILLIS);
204 InputStream is = http.getInputStream();
205 DataInputStream dis = new DataInputStream(is);
207 int responsecode = http.getResponseCode();
208 if (responsecode != HttpURLConnection.HTTP_OK) {
209 // TODO: Remove this print
210 // System.out.println("Code: " + responsecode);
211 throw new ServerException("getSlots failed");
214 byte[] resptype = new byte[7];
215 dis.readFully(resptype);
216 if (!Arrays.equals(resptype, "getslot".getBytes()))
217 throw new Error("Bad Response: " + new String(resptype));
219 return processSlots(dis);
220 } catch (Exception e) {
221 // e.printStackTrace();
222 throw new ServerException("getSlots failed");
226 public boolean hasConnection() {
228 InetAddress address = InetAddress.getByName(hostname);
229 return address.isReachable(TIMEOUT_MILLIS);
230 } catch (Exception e) {
236 * Method that actually handles building Slot objects from the
237 * server response. Shared by both putSlot and getSlots.
239 private Slot[] processSlots(DataInputStream dis) throws Exception {
240 int numberofslots = dis.readInt();
241 int[] sizesofslots = new int[numberofslots];
242 Slot[] slots = new Slot[numberofslots];
243 for (int i = 0; i < numberofslots; i++)
244 sizesofslots[i] = dis.readInt();
246 for (int i = 0; i < numberofslots; i++) {
247 byte[] data = new byte[sizesofslots[i]];
250 data = decryptCipher.doFinal(data);
252 slots[i] = Slot.decode(table, data, mac);