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;
27 * Empty Constructor needed for child class.
34 * Constructor for actual use. Takes in the url and password.
37 CloudComm(String _baseurl, String password) {
38 this.baseurl=_baseurl;
43 * Generates Key from password.
46 private void initKey(String password) {
48 salt=new byte[SALT_SIZE];
49 PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
50 SecretKey tmpkey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
51 this.key = new SecretKeySpec(tmpkey.getEncoded(), "AES");
52 } catch (Exception e) {
54 throw new Error("Failed generating key.");
59 * Inits the HMAC generator.
62 private void initCloud(String password) {
65 mac = Mac.getInstance("HmacSHA256");
67 } catch (Exception e) {
69 throw new Error("Failed To Initialize Ciphers");
74 * Builds the URL for the given request.
77 private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
78 String reqstring=isput?"req=putslot":"req=getslot";
79 String urlstr=baseurl+"?"+reqstring+"&seq="+sequencenumber;
81 urlstr += "&max="+maxentries;
82 return new URL(urlstr);
86 * API for putting a slot into the queue. Returns null on success.
87 * On failure, the server will send slots with newer sequence
91 public Slot[] putSlot(Slot slot, int max) {
93 long sequencenumber=slot.getSequenceNumber();
94 byte[] bytes=slot.encode(mac);
96 URL url=buildRequest(true, sequencenumber, max);
97 URLConnection con=url.openConnection();
98 HttpURLConnection http = (HttpURLConnection) con;
99 http.setRequestMethod("POST");
100 http.setFixedLengthStreamingMode(bytes.length);
101 http.setDoOutput(true);
103 OutputStream os=http.getOutputStream();
106 InputStream is=http.getInputStream();
107 DataInputStream dis=new DataInputStream(is);
108 byte[] resptype=new byte[7];
109 dis.readFully(resptype);
110 if (Arrays.equals(resptype, "getslot".getBytes()))
111 return processSlots(dis);
112 else if (Arrays.equals(resptype, "putslot".getBytes()))
115 throw new Error("Bad response to putslot");
116 } catch (Exception e) {
117 throw new Error("putSlot failed");
122 Cipher encryptCipher =
123 Cipher.getInstance("AES/CBC/PKCS5Padding");
124 encryptCipher.init(Cipher.ENCRYPT_MODE, secret);
125 Cipher decryptCipher =
126 Cipher.getInstance("AES/CBC/PKCS5Padding");
127 decryptCipher.init(Cipher.DECRYPT_MODE, secret);
131 * Request the server to send all slots with the given
132 * sequencenumber or newer.
135 public Slot[] getSlots(long sequencenumber) {
137 URL url=buildRequest(false, sequencenumber, 0);
138 URLConnection con=url.openConnection();
139 HttpURLConnection http = (HttpURLConnection) con;
140 http.setRequestMethod("POST");
142 InputStream is=http.getInputStream();
144 DataInputStream dis=new DataInputStream(is);
145 byte[] resptype=new byte[7];
146 dis.readFully(resptype);
147 if (!Arrays.equals(resptype, "getslot".getBytes()))
148 throw new Error("Bad Response: "+new String(resptype));
150 return processSlots(dis);
151 } catch (Exception e) {
152 throw new Error("getSlots failed");
157 * Method that actually handles building Slot objects from the
158 * server response. Shared by both putSlot and getSlots.
161 private Slot[] processSlots(DataInputStream dis) throws IOException {
162 int numberofslots=dis.readInt();
163 int[] sizesofslots=new int[numberofslots];
164 Slot[] slots=new Slot[numberofslots];
165 for(int i=0; i<numberofslots; i++)
166 sizesofslots[i]=dis.readInt();
168 for(int i=0; i<numberofslots; i++) {
169 byte[] data=new byte[sizesofslots[i]];
171 slots[i]=Slot.decode(data, mac);