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