edits
[iotcloud.git] / version2 / src / C / CloudComm.cpp
1 #include "CloudComm.h"
2 #include "TimingSingleton.h"
3 #include "SecureRandom.h"
4 #include "IoTString.h"
5 #include "Error.h"
6 #include "URL.h"
7 #include "Mac.h"
8 #include "Table.h"
9 #include "Slot.h"
10 #include "Crypto.h"
11 #include "ByteBuffer.h"
12 #include "aes.h"
13 #include <sys/types.h>
14 //#include <sys/socket.h>
15 //#include <arpa/inet.h>
16 //#include <netinet/tcp.h>
17 #include <unistd.h>
18 //#include <netdb.h>
19
20 /**
21  * Empty Constructor needed for child class.
22  */
23 CloudComm::CloudComm() :
24         baseurl(NULL),
25         key(NULL),
26         mac(NULL),
27         password(NULL),
28         random(NULL),
29         salt(NULL),
30         table(NULL),
31         listeningPort(-1),
32         doEnd(false),
33         timer(TimingSingleton_getInstance()),
34         getslot(new Array<char>("getslot", 7)),
35         putslot(new Array<char>("putslot", 7))
36 {
37 }
38
39 /**
40  * Constructor for actual use. Takes in the url and password.
41  */
42 CloudComm::CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password, int _listeningPort) :
43         baseurl(new IoTString(_baseurl)),
44         key(NULL),
45         mac(NULL),
46         password(new IoTString(_password)),
47         random(new SecureRandom()),
48         salt(NULL),
49         table(_table),
50         listeningPort(_listeningPort),
51         doEnd(false),
52         timer(TimingSingleton_getInstance()),
53         getslot(new Array<char>("getslot", 7)),
54         putslot(new Array<char>("putslot", 7)) {
55         /*      if (listeningPort > 0) {
56                 pthread_create(&localServerThread, NULL, threadWrapper, this);
57                 }*/
58 }
59
60 CloudComm::~CloudComm() {
61         delete getslot;
62         delete putslot;
63         if (salt)
64                 delete salt;
65         if (password)
66                 delete password;
67         if (random)
68                 delete random;
69         if (baseurl)
70                 delete baseurl;
71         if (mac)
72                 delete mac;
73         if (key)
74                 delete key;
75 }
76
77 /**
78  * Generates Key from password.
79  */
80 AESKey *CloudComm::initKey() {
81         AESKey *key = new AESKey(password->internalBytes(),
82                                                                                                          salt,
83                                                                                                          65536,
84                                                                                                          128);
85         return key;
86 }
87
88 /**
89  * Inits all the security stuff
90  */
91
92 void CloudComm::initSecurity() {
93         // try to get the salt and if one does not exist set one
94         if (!getSalt()) {
95                 //Set the salt
96                 setSalt();
97         }
98
99         initCrypt();
100 }
101
102 /**
103  * Inits the HMAC generator.
104  */
105 void CloudComm::initCrypt() {
106         if (password == NULL) {
107                 return;
108         }
109         key = initKey();
110         delete password;
111         password = NULL;// drop password
112         mac = new Mac();
113         mac->init(key);
114 }
115
116 /*
117  * Builds the URL for the given request.
118  */
119 IoTString *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries) {
120         const char *reqstring = isput ? "req=putslot" : "req=getslot";
121         char *buffer = (char *) malloc(baseurl->length() + 200);
122         memcpy(buffer, baseurl->internalBytes()->internalArray(), baseurl->length());
123         int offset = baseurl->length();
124         offset += sprintf(&buffer[offset], "?%s&seq=%" PRId64, reqstring, sequencenumber);
125         if (maxentries != 0)
126                 sprintf(&buffer[offset], "&max=%" PRId64, maxentries);
127         IoTString *urlstr = new IoTString(buffer);
128         free(buffer);
129         return urlstr;
130 }
131
132 void loopWrite(TCPClient * client, char *array, int bytestowrite) {
133         int byteswritten = 0;
134         while (bytestowrite) {
135                 int bytes = client->write((const unsigned char *) &array[byteswritten], bytestowrite);
136                 if (bytes >= 0) {
137                         byteswritten += bytes;
138                         bytestowrite -= bytes;
139                 } else {
140                         //printf("Error in write\n");
141                         exit(-1);
142                 }
143         }
144 }
145
146 void loopRead(TCPClient * client, char *array, int bytestoread) {
147         int bytesread = 0;
148         while (bytestoread) {
149                 int bytes = client->read((unsigned char *) &array[bytesread], bytestoread);
150                 if (bytes >= 0) {
151                         bytesread += bytes;
152                         bytestoread -= bytes;
153                 } else {
154                         //printf("Error in read\n");
155                         exit(-1);
156                 }
157         }
158 }
159
160 WebConnection openURL(IoTString *url) {
161         if (url->length() < 7 || memcmp(url->internalBytes()->internalArray(), "http://", 7)) {
162                 //printf("BOGUS URL\n");
163                 exit(-1);
164         }
165         int i = 7;
166         for (; i < url->length(); i++)
167                 if (url->get(i) == '/')
168                         break;
169
170         if ( i == url->length()) {
171                 //printf("ERROR in openURL\n");
172                 exit(-1);
173         }
174
175         char *host = (char *) malloc(i - 6);
176         memcpy(host, &url->internalBytes()->internalArray()[7], i - 7);
177         host[i - 7] = 0;
178         //printf("%s\n", host);
179
180         char *message = (char *)malloc(sizeof("POST  HTTP/1.1\r\n") + sizeof("Host: \r\n") + 2 * url->length());
181
182         /* fill in the parameters */
183         int post = sprintf(message,"POST ");
184         /* copy data */
185         memcpy(&message[post], &url->internalBytes()->internalArray()[i], url->length() - i);
186         int endpost = sprintf(&message[post + url->length() - i], " HTTP/1.1\r\n");
187
188         int hostlen = sprintf(&message[endpost + post + url->length() - i], "Host: ");
189         memcpy(&message[endpost + post + url->length() + hostlen - i], host, i - 7);
190         sprintf(&message[endpost + post + url->length() + hostlen - 7], "\r\n");
191
192
193         WebConnection wc;
194         wc.numBytes = -1;
195
196         if (!wc.client.connect(host, 80)) {
197                 myerror("ERROR connecting\n");
198         }
199         free(host);
200         
201         /* send the request */
202         int total = strlen(message);
203         loopWrite(&wc.client, message, total);
204         free(message);
205         return wc;
206 }
207
208 TCPClient createSocket(IoTString *name, int port) {
209         char *host = (char *) malloc(name->length() + 1);
210         memcpy(host, name->internalBytes()->internalArray(), name->length());
211         host[name->length()] = 0;
212         //printf("%s\n", host);
213
214         /* lookup the ip address */
215         TCPClient client;
216         if (!client.connect(host, port)) {
217                 myerror("ERROR connecting\n");
218         }
219
220         free(host);
221         return client;
222 }
223
224 void writeSocketData(TCPClient * fd, Array<char> *data) {
225         loopWrite(fd, data->internalArray(), data->length());
226 }
227
228 void writeSocketInt(TCPClient * fd, int32_t value) {
229         char array[4];
230         array[0] = value >> 24;
231         array[1] = (value >> 16) & 0xff;
232         array[2] = (value >> 8) & 0xff;
233         array[3] = value & 0xff;
234         loopWrite(fd, array, 4);
235 }
236
237 int readSocketInt(TCPClient * fd) {
238         char array[4];
239         loopRead(fd, array, 4);
240         return (((int32_t)(unsigned char) array[0]) << 24) |
241                                  (((int32_t)(unsigned char) array[1]) << 16) |
242                                  (((int32_t)(unsigned char) array[2]) << 8) |
243                                  ((int32_t)(unsigned char) array[3]);
244 }
245
246 void readSocketData(TCPClient * fd, Array<char> *data) {
247         loopRead(fd, data->internalArray(), data->length());
248 }
249
250 void writeURLDataAndClose(WebConnection *wc, Array<char> *data) {
251         char buffer[300];
252         sprintf(buffer, "Content-Length: %d\r\n\r\n", data->length());
253         wc->client.print(buffer);
254         loopWrite(&wc->client, data->internalArray(), data->length());
255 }
256
257 void closeURLReq(WebConnection *wc) {
258         wc->client.println("");
259 }
260
261 void readURLData(WebConnection *wc, Array<char> *output) {
262         loopRead(&wc->client, output->internalArray(), output->length());
263 }
264
265 int readURLInt(WebConnection *wc) {
266         char array[4];
267         loopRead(&wc->client, array, 4);
268         return (((int32_t)(unsigned char) array[0]) << 24) |
269                                  (((int32_t)(unsigned char) array[1]) << 16) |
270                                  (((int32_t)(unsigned char) array[2]) << 8) |
271                                  ((int32_t)(unsigned char) array[3]);
272 }
273
274 void readLine(WebConnection *wc, char *response, int numBytes) {
275         int offset = 0;
276         char newchar;
277         while (true) {
278                 int bytes = wc->client.read((unsigned char *) &newchar, 1);
279                 if (bytes <= 0)
280                         break;
281                 if (offset == (numBytes - 1)) {
282                         //printf("Response too long");
283                         exit(-1);
284                 }
285                 response[offset++] = newchar;
286                 if (newchar == '\n')
287                         break;
288         }
289         response[offset] = 0;
290 }
291
292 int getResponseCode(WebConnection *wc) {
293         char response[600];
294         readLine(wc, response, sizeof(response));
295         int ver1 = 0, ver2 = 0, respcode = 0;
296         sscanf(response, "HTTP/%d.%d %d", &ver1, &ver2, &respcode);
297         //printf("Response code %d\n", respcode);
298         return respcode;
299 }
300
301 void readHeaders(WebConnection *wc) {
302         char response[600];
303         int numBytes;
304
305         while (true) {
306                 readLine(wc, response, sizeof(response));
307                 if (response[0] == '\r')
308                         return;
309                 else if (memcmp(response, "Content-Length:", sizeof("Content-Length:") - 1) == 0) {
310                         sscanf(response, "Content-Length: %d", &numBytes);
311                         wc->numBytes = numBytes;
312                 }
313         }
314 }
315
316 void CloudComm::setSalt() {
317         if (salt != NULL) {
318                 // Salt already sent to server so don't set it again
319                 return;
320         }
321
322         WebConnection wc = {-1, -1};
323         //      try {
324                 Array<char> *saltTmp = new Array<char>(CloudComm_SALT_SIZE);
325                 random->nextBytes(saltTmp);
326
327                 char *buffer = (char *) malloc(baseurl->length() + 100);
328                 memcpy(buffer, baseurl->internalBytes()->internalArray(), baseurl->length());
329                 int offset = baseurl->length();
330                 offset += sprintf(&buffer[offset], "?req=setsalt");
331                 IoTString *urlstr = new IoTString(buffer);
332                 free(buffer);
333
334                 timer->startTime();
335                 wc = openURL(urlstr);
336                 delete urlstr;
337                 writeURLDataAndClose(&wc, saltTmp);
338
339                 int responsecode = getResponseCode(&wc);
340                 if (responsecode != HttpURLConnection_HTTP_OK) {
341                         //throw new Error("Invalid response");
342                         myerror("Invalid response\n");
343                 }
344                 wc.client.stop();
345
346                 timer->endTime();
347                 salt = saltTmp;
348                 /*      } catch (Exception *e) {
349                 timer->endTime();
350                 throw new ServerException("Failed setting salt", ServerException_TypeConnectTimeout);
351                 }*/
352 }
353
354 bool CloudComm::getSalt() {
355         WebConnection wc;
356         wc.numBytes = -1;
357         IoTString *urlstr = NULL;
358
359         //      try {
360                 char *buffer = (char *) malloc(baseurl->length() + 100);
361                 memcpy(buffer, baseurl->internalBytes()->internalArray(), baseurl->length());
362                 int offset = baseurl->length();
363                 offset += sprintf(&buffer[offset], "?req=getsalt");
364                 urlstr = new IoTString(buffer);
365                 free(buffer);
366                 /*      } catch (Exception *e) {
367                 throw new Error("getSlot failed");
368                 }*/
369                 //      try {
370                 timer->startTime();
371                 wc = openURL(urlstr);
372                 delete urlstr;
373                 urlstr = NULL;
374                 closeURLReq(&wc);
375                 timer->endTime();
376                 /*      } catch (SocketTimeoutException *e) {
377                 if (urlstr)
378                         delete urlstr;
379                 timer->endTime();
380                 throw new ServerException("getSalt failed", ServerException_TypeConnectTimeout);
381         } catch (Exception *e) {
382                 if (urlstr)
383                         delete urlstr;
384                 throw new Error("getSlot failed");
385                 }*/
386
387                 //      try {
388                 timer->startTime();
389                 int responsecode = getResponseCode(&wc);
390                 readHeaders(&wc);
391                 if (responsecode != HttpURLConnection_HTTP_OK) {
392                         //throw new Error("Invalid response");
393                         myerror("Invalid response\n");
394                 }
395                 if (wc.numBytes == 0) {
396                         timer->endTime();
397                         wc.client.stop();
398                         return false;
399                 }
400
401
402                 int salt_length = readURLInt(&wc);
403                 Array<char> *tmp = new Array<char>(salt_length);
404                 readURLData(&wc, tmp);
405                 wc.client.stop();
406
407                 salt = tmp;
408                 timer->endTime();
409                 return true;
410                 /*      } catch (SocketTimeoutException *e) {
411                 timer->endTime();
412                 throw new ServerException("getSalt failed", ServerException_TypeInputTimeout);
413         } catch (Exception *e) {
414                 throw new Error("getSlot failed");
415                 }*/
416 }
417
418 Array<char> *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber) {
419         ByteBuffer *buffer = ByteBuffer_allocate(CloudComm_IV_SIZE);
420         buffer->putLong(machineId);
421         int64_t localSequenceNumberShifted = localSequenceNumber << 16;
422         buffer->putLong(localSequenceNumberShifted);
423         return buffer->array();
424 }
425
426 Array<char> *AESEncrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
427         Array<char> *output = new Array<char>(data->length());
428         aes_encrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *) output->internalArray(), (WORD *)key->getKeySchedule(), key->getKey()->length() * 8, (BYTE *)ivBytes->internalArray());
429         return output;
430 }
431
432 Array<char> *AESDecrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
433         Array<char> *output = new Array<char>(data->length());
434         aes_decrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *)output->internalArray(), (WORD *)key->getKeySchedule(), key->getKey()->length() * 8, (BYTE *)ivBytes->internalArray());
435         return output;
436 }
437
438 Array<char> *CloudComm::encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes) {
439         //      try {
440                 Array<char> *encryptedBytes = AESEncrypt(ivBytes, key, rawData);
441                 Array<char> *chars = new Array<char>(encryptedBytes->length() + CloudComm_IV_SIZE);
442                 System_arraycopy(ivBytes, 0, chars, 0, ivBytes->length());
443                 System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes->length());
444                 delete encryptedBytes;
445                 return chars;
446                 /*      } catch (Exception *e) {
447                 throw new Error("Failed To Encrypt");
448                 }*/
449 }
450
451 Array<char> *CloudComm::stripIVAndDecryptSlot(Array<char> *rawData) {
452         //      try {
453                 Array<char> *ivBytes = new Array<char>(CloudComm_IV_SIZE);
454                 Array<char> *encryptedBytes = new Array<char>(rawData->length() - CloudComm_IV_SIZE);
455                 System_arraycopy(rawData, 0, ivBytes, 0, CloudComm_IV_SIZE);
456                 System_arraycopy(rawData, CloudComm_IV_SIZE, encryptedBytes, 0, encryptedBytes->length());
457                 Array<char> * data = AESDecrypt(ivBytes, key, encryptedBytes);
458                 delete encryptedBytes;
459                 delete ivBytes;
460                 return data;
461                 /*      } catch (Exception *e) {
462                 throw new Error("Failed To Decrypt");
463                 }*/
464 }
465
466 /*
467  * API for putting a slot into the queue.  Returns NULL on success.
468  * On failure, the server will send slots with newer sequence
469  * numbers.
470  */
471 Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
472         WebConnection wc = {-1, -1};
473         //      try {
474                 if (salt == NULL) {
475                         if (!getSalt()) {
476                                 //                              throw new ServerException("putSlot failed", ServerException_TypeSalt);
477                                 myerror("putSlot failed\n");
478                         }
479                         initCrypt();
480                 }
481
482                 int64_t sequencenumber = slot->getSequenceNumber();
483                 Array<char> *slotBytes = slot->encode(mac);
484                 Array<char> * ivBytes = slot->getSlotCryptIV();
485                 Array<char> *chars = encryptSlotAndPrependIV(slotBytes, ivBytes);
486                 delete ivBytes;
487                 delete slotBytes;
488                 IoTString *url = buildRequest(true, sequencenumber, max);
489                 timer->startTime();
490                 wc = openURL(url);
491                 delete url;
492                 writeURLDataAndClose(&wc, chars);
493                 delete chars;
494                 timer->endTime();
495                 /*      } catch (ServerException *e) {
496                 timer->endTime();
497                 throw e;
498         } catch (SocketTimeoutException *e) {
499                 timer->endTime();
500                 throw new ServerException("putSlot failed", ServerException_TypeConnectTimeout);
501         } catch (Exception *e) {
502                 throw new Error("putSlot failed");
503                 }*/
504
505         Array<char> *resptype = NULL;
506         //      try {
507                 int respcode = getResponseCode(&wc);
508                 readHeaders(&wc);
509                 timer->startTime();
510                 resptype = new Array<char>(7);
511                 readURLData(&wc, resptype);
512                 timer->endTime();
513
514                 if (resptype->equals(getslot)) {
515                         delete resptype;
516                         Array<Slot *> *tmp = processSlots(&wc);
517                         wc.client.stop();
518                         return tmp;
519                 } else if (resptype->equals(putslot)) {
520                         delete resptype;
521                         wc.client.stop();
522                         return NULL;
523                 } else {
524                         delete resptype;
525                         wc.client.stop();
526                         //throw new Error("Bad response to putslot");
527                         myerror("Bad response to putslot\n");
528                 }
529                 /*      } catch (SocketTimeoutException *e) {
530                 if (resptype != NULL)
531                         delete resptype;
532                 timer->endTime();
533                 close(wc.fd);
534                 throw new ServerException("putSlot failed", ServerException_TypeInputTimeout);
535         } catch (Exception *e) {
536                 if (resptype != NULL)
537                         delete resptype;
538                 throw new Error("putSlot failed");
539                 }*/
540 }
541
542 /**
543  * Request the server to send all slots with the given
544  * sequencenumber or newer->
545  */
546 Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
547         WebConnection wc = {-1, -1};
548         //      try {
549                 if (salt == NULL) {
550                         if (!getSalt()) {
551                                 //throw new ServerException("getSlots failed", ServerException_TypeSalt);
552                                 myerror("getSlots failed\n");
553                         }
554                         initCrypt();
555                 }
556
557                 IoTString *url = buildRequest(false, sequencenumber, 0);
558                 timer->startTime();
559                 wc = openURL(url);
560                 delete url;
561                 closeURLReq(&wc);
562                 timer->endTime();
563                 /*      } catch (SocketTimeoutException *e) {
564                 timer->endTime();
565                 throw new ServerException("getSlots failed", ServerException_TypeConnectTimeout);
566         } catch (ServerException *e) {
567                 timer->endTime();
568
569                 throw e;
570         } catch (Exception *e) {
571                 throw new Error("getSlots failed");
572                 }*/
573
574                 //      try {
575                 timer->startTime();
576                 int responsecode = getResponseCode(&wc);
577                 readHeaders(&wc);
578                 Array<char> *resptype = new Array<char>(7);
579                 readURLData(&wc, resptype);
580                 timer->endTime();
581                 if (!resptype->equals(getslot))
582                         //                      throw new Error("Bad Response: ");
583                         myerror("Bad Response: \n");
584
585                 delete resptype;
586                 Array<Slot *> *tmp = processSlots(&wc);
587                 wc.client.stop();
588                 return tmp;
589                 /*      } catch (SocketTimeoutException *e) {
590                 timer->endTime();
591                 close(wc.fd);
592                 throw new ServerException("getSlots failed", ServerException_TypeInputTimeout);
593         } catch (Exception *e) {
594                 throw new Error("getSlots failed");
595                 }*/
596 }
597
598 /**
599  * Method that actually handles building Slot objects from the
600  * server response.  Shared by both putSlot and getSlots.
601  */
602 Array<Slot *> *CloudComm::processSlots(WebConnection *wc) {
603         int numberofslots = readURLInt(wc);
604         Array<int> *sizesofslots = new Array<int>(numberofslots);
605         Array<Slot *> *slots = new Array<Slot *>(numberofslots);
606
607         for (int i = 0; i < numberofslots; i++)
608                 sizesofslots->set(i, readURLInt(wc));
609         for (int i = 0; i < numberofslots; i++) {
610                 Array<char> *rawData = new Array<char>(sizesofslots->get(i));
611                 readURLData(wc, rawData);
612                 Array<char> *data = stripIVAndDecryptSlot(rawData);
613                 delete rawData;
614                 slots->set(i, Slot_decode(table, data, mac));
615                 delete data;
616         }
617         delete sizesofslots;
618         return slots;
619 }
620
621 Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString *host, int port) {
622         if (salt == NULL)
623                 return NULL;
624         //      try {
625         //printf("Passing Locally\n");
626                 mac->update(sendData, 0, sendData->length());
627                 Array<char> *genmac = mac->doFinal();
628                 Array<char> *totalData = new Array<char>(sendData->length() + genmac->length());
629                 System_arraycopy(sendData, 0, totalData, 0, sendData->length());
630                 System_arraycopy(genmac, 0, totalData, sendData->length(), genmac->length());
631
632                 // Encrypt the data for sending
633                 Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
634                 Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
635
636                 // Open a TCP socket connection to a local device
637                 TCPClient socket = createSocket(host, port);
638
639                 timer->startTime();
640                 // Send data to output (length of data, the data)
641                 writeSocketInt(&socket, encryptedData->length());
642                 writeSocketData(&socket, encryptedData);
643
644                 int lengthOfReturnData = readSocketInt(&socket);
645                 Array<char> *returnData = new Array<char>(lengthOfReturnData);
646                 readSocketData(&socket, returnData);
647                 timer->endTime();
648                 returnData = stripIVAndDecryptSlot(returnData);
649
650                 // We are done with this socket
651                 socket.stop();
652                 mac->update(returnData, 0, returnData->length() - CloudComm_HMAC_SIZE);
653                 Array<char> *realmac = mac->doFinal();
654                 Array<char> *recmac = new Array<char>(CloudComm_HMAC_SIZE);
655                 System_arraycopy(returnData, returnData->length() - realmac->length(), recmac, 0, realmac->length());
656
657                 if (!recmac->equals(realmac))
658                         //                      throw new Error("Local Error: Invalid HMAC!  Potential Attack!");
659                         myerror("Local Error: Invalid HMAC!  Potential Attack!\n");
660
661                 Array<char> *returnData2 = new Array<char>(lengthOfReturnData - recmac->length());
662                 System_arraycopy(returnData, 0, returnData2, 0, returnData2->length());
663
664                 return returnData2;
665                 /*      } catch (Exception *e) {
666                 printf("Exception\n");
667                 }*/
668
669         return NULL;
670 }
671
672 void CloudComm::closeCloud() {
673         doEnd = true;
674
675         /*      if (listeningPort > 0) {
676                 if (pthread_join(localServerThread, NULL) != 0)
677                         throw new Error("Local Server thread join issue...");
678                         }*/
679 }