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) {
115 throw new ServerException("Failed setting salt");
120 private void getSalt() throws Exception {
121 URL url = new URL(baseurl + "?req=getsalt");
122 URLConnection con = url.openConnection();
123 HttpURLConnection http = (HttpURLConnection) con;
124 http.setRequestMethod("POST");
127 InputStream is = http.getInputStream();
128 DataInputStream dis = new DataInputStream(is);
129 int salt_length = dis.readInt();
130 byte [] tmp = new byte[salt_length];
136 * API for putting a slot into the queue. Returns null on success.
137 * On failure, the server will send slots with newer sequence
141 Slot[] putSlot(Slot slot, int max) throws ServerException {
148 long sequencenumber = slot.getSequenceNumber();
149 byte[] bytes = slot.encode(mac);
150 bytes = encryptCipher.doFinal(bytes);
153 URL url = buildRequest(true, sequencenumber, max);
154 URLConnection con = url.openConnection();
155 HttpURLConnection http = (HttpURLConnection) con;
157 http.setRequestMethod("POST");
158 http.setFixedLengthStreamingMode(bytes.length);
159 http.setDoOutput(true);
160 http.setConnectTimeout(TIMEOUT_MILLIS);
161 // http.setReadTimeout(TIMEOUT_MILLIS);
164 OutputStream os = http.getOutputStream();
169 InputStream is = http.getInputStream();
170 DataInputStream dis = new DataInputStream(is);
171 byte[] resptype = new byte[7];
172 dis.readFully(resptype);
174 if (Arrays.equals(resptype, "getslot".getBytes()))
175 return processSlots(dis);
176 else if (Arrays.equals(resptype, "putslot".getBytes()))
179 throw new Error("Bad response to putslot");
181 } catch (Exception e) {
182 throw new ServerException("putSlot failed");
188 * Request the server to send all slots with the given
189 * sequencenumber or newer.
191 Slot[] getSlots(long sequencenumber) throws ServerException {
198 URL url = buildRequest(false, sequencenumber, 0);
199 URLConnection con = url.openConnection();
200 HttpURLConnection http = (HttpURLConnection) con;
201 http.setRequestMethod("POST");
202 http.setConnectTimeout(TIMEOUT_MILLIS);
203 // http.setReadTimeout(TIMEOUT_MILLIS);
205 InputStream is = http.getInputStream();
206 DataInputStream dis = new DataInputStream(is);
208 int responsecode = http.getResponseCode();
209 if (responsecode != HttpURLConnection.HTTP_OK) {
210 // TODO: Remove this print
211 // System.out.println("Code: " + responsecode);
212 throw new ServerException("getSlots failed");
215 byte[] resptype = new byte[7];
216 dis.readFully(resptype);
217 if (!Arrays.equals(resptype, "getslot".getBytes()))
218 throw new Error("Bad Response: " + new String(resptype));
220 return processSlots(dis);
221 } catch (Exception e) {
222 // e.printStackTrace();
223 throw new ServerException("getSlots failed");
227 public boolean hasConnection() {
229 InetAddress address = InetAddress.getByName(hostname);
230 return address.isReachable(TIMEOUT_MILLIS);
231 } catch (Exception e) {
237 * Method that actually handles building Slot objects from the
238 * server response. Shared by both putSlot and getSlots.
240 private Slot[] processSlots(DataInputStream dis) throws Exception {
241 int numberofslots = dis.readInt();
242 int[] sizesofslots = new int[numberofslots];
243 Slot[] slots = new Slot[numberofslots];
244 for (int i = 0; i < numberofslots; i++)
245 sizesofslots[i] = dis.readInt();
247 for (int i = 0; i < numberofslots; i++) {
248 byte[] data = new byte[sizesofslots[i]];
251 data = decryptCipher.doFinal(data);
253 slots[i] = Slot.decode(table, data, mac);