Offline support added
[iotcloud.git] / version2 / src / java / iotcloud / CloudComm.java
1 package iotcloud;
2 import java.io.*;
3 import java.net.*;
4 import java.util.Arrays;
5 import javax.crypto.*;
6 import javax.crypto.spec.*;
7 import java.security.SecureRandom;
8
9 /**
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>
13  * @version 1.0
14  */
15
16
17 class CloudComm {
18         String baseurl;
19         Cipher encryptCipher;
20         Cipher decryptCipher;
21         Mac mac;
22         String password;
23         SecureRandom random;
24         static final int SALT_SIZE = 8;
25         static final int TIMEOUT_MILLIS = 100;
26         byte salt[];
27         Table table;
28
29         /**
30          * Empty Constructor needed for child class.
31          */
32         CloudComm() {
33         }
34
35         /**
36          * Constructor for actual use. Takes in the url and password.
37          */
38         CloudComm(Table _table, String _baseurl, String _password) {
39                 this.table = _table;
40                 this.baseurl = _baseurl;
41                 this.password = _password;
42                 this.random = new SecureRandom();
43         }
44
45         /**
46          * Generates Key from password.
47          */
48         private SecretKeySpec initKey() {
49                 try {
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) {
54                         e.printStackTrace();
55                         throw new Error("Failed generating key.");
56                 }
57         }
58
59         /**
60          * Inits the HMAC generator.
61          */
62         private void initCrypt() {
63                 try {
64                         SecretKeySpec key = initKey();
65                         password = null; // drop password
66                         mac = Mac.getInstance("HmacSHA256");
67                         mac.init(key);
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) {
73                         e.printStackTrace();
74                         throw new Error("Failed To Initialize Ciphers");
75                 }
76         }
77
78         /*
79          * Builds the URL for the given request.
80          */
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;
84                 if (maxentries != 0)
85                         urlstr += "&max=" + maxentries;
86                 return new URL(urlstr);
87         }
88
89         public void setSalt() throws ServerException {
90                 try {
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);
100                         http.connect();
101                         OutputStream os = http.getOutputStream();
102                         os.write(saltTmp);
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");
108                         }
109
110                         salt = saltTmp;
111                 } catch (Exception e) {
112                         e.printStackTrace();
113                         throw new ServerException("Failed setting salt");
114                 }
115                 initCrypt();
116         }
117
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");
123                 http.connect();
124
125                 InputStream is = http.getInputStream();
126                 DataInputStream dis = new DataInputStream(is);
127                 int salt_length = dis.readInt();
128                 byte [] tmp = new byte[salt_length];
129                 dis.readFully(tmp);
130                 salt = tmp;
131         }
132
133         /*
134          * API for putting a slot into the queue.  Returns null on success.
135          * On failure, the server will send slots with newer sequence
136          * numbers.
137          */
138
139         Slot[] putSlot(Slot slot, int max) throws ServerException {
140                 try {
141                         if (salt == null) {
142                                 getSalt();
143                                 initCrypt();
144                         }
145
146                         long sequencenumber = slot.getSequenceNumber();
147                         byte[] bytes = slot.encode(mac);
148                         bytes = encryptCipher.doFinal(bytes);
149
150
151                         URL url = buildRequest(true, sequencenumber, max);
152                         URLConnection con = url.openConnection();
153                         HttpURLConnection http = (HttpURLConnection) con;
154
155                         http.setRequestMethod("POST");
156                         http.setFixedLengthStreamingMode(bytes.length);
157                         http.setDoOutput(true);
158                         http.setConnectTimeout(TIMEOUT_MILLIS);
159                         // http.setReadTimeout(TIMEOUT_MILLIS);
160                         http.connect();
161
162                         OutputStream os = http.getOutputStream();
163                         os.write(bytes);
164                         os.flush();
165
166
167                         InputStream is = http.getInputStream();
168                         DataInputStream dis = new DataInputStream(is);
169                         byte[] resptype = new byte[7];
170                         dis.readFully(resptype);
171
172                         if (Arrays.equals(resptype, "getslot".getBytes()))
173                                 return processSlots(dis);
174                         else if (Arrays.equals(resptype, "putslot".getBytes()))
175                                 return null;
176                         else
177                                 throw new Error("Bad response to putslot");
178
179                 } catch (Exception e) {
180                         throw new ServerException("putSlot failed");
181                 }
182         }
183
184
185         /**
186          * Request the server to send all slots with the given
187          * sequencenumber or newer.
188          */
189         Slot[] getSlots(long sequencenumber) throws ServerException {
190                 try {
191                         if (salt == null) {
192                                 getSalt();
193                                 initCrypt();
194                         }
195
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);
202                         http.connect();
203                         InputStream is = http.getInputStream();
204                         DataInputStream dis = new DataInputStream(is);
205
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");
211                         }
212
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));
217                         else
218                                 return processSlots(dis);
219                 } catch (Exception e) {
220                         // e.printStackTrace();
221                         throw new ServerException("getSlots failed");
222                 }
223         }
224
225         /**
226          * Method that actually handles building Slot objects from the
227          * server response.  Shared by both putSlot and getSlots.
228          */
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();
235
236                 for (int i = 0; i < numberofslots; i++) {
237                         byte[] data = new byte[sizesofslots[i]];
238                         dis.readFully(data);
239
240                         data = decryptCipher.doFinal(data);
241
242                         slots[i] = Slot.decode(table, data, mac);
243                 }
244                 dis.close();
245                 return slots;
246         }
247 }