b5aca6242ba2948559bb8dadd1748e6d1052249e
[iotcloud.git] / 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         byte[] salt;
23         SecretKeySpec key;
24         static final int SALT_SIZE = 8;
25
26         /**
27          * Empty Constructor needed for child class.
28          */
29
30         CloudComm() {
31         }
32
33         /**
34          * Constructor for actual use. Takes in the url and password.
35          */
36
37         CloudComm(String _baseurl, String password) {
38                 this.baseurl=_baseurl;
39                 initCloud(password);
40         }
41
42         /**
43          * Generates Key from password.
44          */
45
46         private void initKey(String password) {
47                 try {
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) {
53                         e.printStackTrace();
54                         throw new Error("Failed generating key.");
55                 }
56         }
57
58         /**
59          * Inits the HMAC generator.
60          */
61
62         private void initCloud(String password) {
63                 try {
64                         initKey(password);
65                         mac = Mac.getInstance("HmacSHA256");
66                         mac.init(key);
67                 } catch (Exception e) {
68                         e.printStackTrace();
69                         throw new Error("Failed To Initialize Ciphers");
70                 }
71         }
72
73         /*
74          * Builds the URL for the given request.
75          */
76
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;
80                 if (maxentries != 0)
81                         urlstr += "&max="+maxentries;
82                 return new URL(urlstr);
83         }
84
85         /*
86          * API for putting a slot into the queue.  Returns null on success.
87          * On failure, the server will send slots with newer sequence
88          * numbers.
89          */
90
91         public Slot[] putSlot(Slot slot, int max) {
92                 try {
93                         long sequencenumber=slot.getSequenceNumber();
94                         byte[] bytes=slot.encode(mac);
95
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);
102                         http.connect();
103                         OutputStream os=http.getOutputStream();
104                         os.write(bytes);
105
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()))
113                                 return null;
114                         else
115                                 throw new Error("Bad response to putslot");
116                 } catch (Exception e) {
117                         throw new Error("putSlot failed");
118                 }
119         }
120
121         /*
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);
128          */
129
130         /**
131          * Request the server to send all slots with the given
132          * sequencenumber or newer.
133          */
134
135         public Slot[] getSlots(long sequencenumber) {
136                 try {
137                         URL url=buildRequest(false, sequencenumber, 0);
138                         URLConnection con=url.openConnection();
139                         HttpURLConnection http = (HttpURLConnection) con;
140                         http.setRequestMethod("POST");
141                         http.connect();
142                         InputStream is=http.getInputStream();
143
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));
149                         else
150                                 return processSlots(dis);
151                 } catch (Exception e) {
152                         throw new Error("getSlots failed");
153                 }
154         }
155
156         /**
157          * Method that actually handles building Slot objects from the
158          * server response.  Shared by both putSlot and getSlots.
159          */
160
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();
167
168                 for(int i=0; i<numberofslots; i++) {
169                         byte[] data=new byte[sizesofslots[i]];
170                         dis.readFully(data);
171                         slots[i]=Slot.decode(data, mac);
172                 }
173                 dis.close();
174                 return slots;
175         }
176 }