From 1a486d4bea2b57d36f6fc0e40193bb7812405c4f Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 2 Mar 2018 00:32:08 -0800 Subject: [PATCH] Compiles --- version2/src/C/CloudComm.cc | 131 +++++++++++++++++++----------------- version2/src/C/CloudComm.h | 6 +- version2/src/C/Error.h | 6 ++ version2/src/C/Table.cc | 2 +- version2/src/C/array.h | 2 +- 5 files changed, 81 insertions(+), 66 deletions(-) diff --git a/version2/src/C/CloudComm.cc b/version2/src/C/CloudComm.cc index 06d6a89..4e4000f 100644 --- a/version2/src/C/CloudComm.cc +++ b/version2/src/C/CloudComm.cc @@ -6,8 +6,11 @@ #include "URL.h" #include "Mac.h" #include "Table.h" +#include "Slot.h" #include "Crypto.h" #include "ByteBuffer.h" +#include +#include /** * Empty Constructor needed for child class. @@ -23,7 +26,9 @@ CloudComm::CloudComm() : listeningPort(-1), localServerThread(NULL), doEnd(false), - timer(TimingSingleton_getInstance()) + timer(TimingSingleton_getInstance()), + getslot(new Array("getslot", 7)), + putslot(new Array("putslot", 7)) { } @@ -118,6 +123,26 @@ int openURL(IoTString *url, bool isPost) { return 0; } +int createSocket(IoTString *host, int port) { + return 0; +} + +int createSocket(int port) { + return 0; +} + +int acceptSocket(int socket) { + return 0; +} + +void writeSocketData(int fd, Array *data) {} + +void writeSocketInt(int fd, int value) {} + +int readSocketInt(int fd) {return 0;} + +void readSocketData(int fd, Array *data) {} + void writeURLData(int fd, Array *data) { } @@ -198,17 +223,12 @@ bool CloudComm::getSalt() { if (responsecode != HttpURLConnection_HTTP_OK) { throw new Error("Invalid response"); } - if (is->available() > 0) { - int salt_length = readURLInt(fd); - Array *tmp = new Array(salt_length); - readURLData(fd, tmp); - salt = tmp; - timer->endTime(); - return true; - } else { - timer->endTime(); - return false; - } + int salt_length = readURLInt(fd); + Array *tmp = new Array(salt_length); + readURLData(fd, tmp); + salt = tmp; + timer->endTime(); + return true; } catch (SocketTimeoutException *e) { timer->endTime(); throw new ServerException("getSalt failed", ServerException_TypeInputTimeout); @@ -225,12 +245,17 @@ Array *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber) return buffer->array(); } +Array *AESEncrypt(Array * ivBytes, AESKey * key, Array * data) { + return NULL; +} + +Array *AESDecrypt(Array * ivBytes, AESKey * key, Array * data) { + return NULL; +} + Array *CloudComm::encryptSlotAndPrependIV(Array *rawData, Array *ivBytes) { try { - IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes); - Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding"); - cipher->init(Cipher_ENCRYPT_MODE, key, ivSpec); - Array *encryptedBytes = cipher->doFinal(rawData); + Array *encryptedBytes = AESEncrypt(ivBytes, key, rawData); Array *chars = new Array(encryptedBytes->length() + CloudComm_IV_SIZE); System_arraycopy(ivBytes, 0, chars, 0, ivBytes->length()); System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes->length()); @@ -247,10 +272,7 @@ Array *CloudComm::stripIVAndDecryptSlot(Array *rawData) { Array *encryptedBytes = new Array(rawData->length() - CloudComm_IV_SIZE); System_arraycopy(rawData, 0, ivBytes, 0, CloudComm_IV_SIZE); System_arraycopy(rawData, CloudComm_IV_SIZE, encryptedBytes, 0, encryptedBytes->length()); - IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes); - Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding"); - cipher->init(Cipher_DECRYPT_MODE, key, ivSpec); - return cipher->doFinal(encryptedBytes); + return AESDecrypt(ivBytes, key, encryptedBytes); } catch (Exception *e) { throw new Error("Failed To Decrypt"); } @@ -295,15 +317,15 @@ Array *CloudComm::putSlot(Slot *slot, int max) { readURLData(fd, resptype); timer->endTime(); - if (resptype->equals("getslot"->getBytes())) { + if (resptype->equals(getslot)) { return processSlots(fd); - } else if (resptype->equals("putslot"->getBytes())) { + } else if (resptype->equals(putslot)) { return NULL; } else throw new Error("Bad response to putslot"); } catch (SocketTimeoutException *e) { timer->endTime(); - throw new ServerException("putSlot failed", ServerException->TypeInputTimeout); + throw new ServerException("putSlot failed", ServerException_TypeInputTimeout); } catch (Exception *e) { throw new Error("putSlot failed"); } @@ -314,6 +336,7 @@ Array *CloudComm::putSlot(Slot *slot, int max) { * sequencenumber or newer-> */ Array *CloudComm::getSlots(int64_t sequencenumber) { + int fd = -1; try { if (salt == NULL) { if (!getSalt()) { @@ -324,12 +347,7 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { IoTString *url = buildRequest(false, sequencenumber, 0); timer->startTime(); - URLConnection *con = url->openConnection(); - HttpURLConnection *http = (HttpURLConnection *) con; - http->setRequestMethod("POST"); - http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS); - http->setReadTimeout(CloudComm_TIMEOUT_MILLIS); - http->connect(); + fd = openURL(url, true); timer->endTime(); } catch (SocketTimeoutException *e) { timer->endTime(); @@ -347,10 +365,10 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { Array *resptype = new Array(7); readURLData(fd, resptype); timer->endTime(); - if (!resptype->equals("getslot"->getBytes())) - throw new Error("Bad Response: " + new String(resptype)); + if (!resptype->equals(getslot)) + throw new Error("Bad Response: "); - return processSlots(dis); + return processSlots(fd); } catch (SocketTimeoutException *e) { timer->endTime(); throw new ServerException("getSlots failed", ServerException_TypeInputTimeout); @@ -372,14 +390,14 @@ Array *CloudComm::processSlots(int fd) { sizesofslots->set(i, readURLInt(fd)); for (int i = 0; i < numberofslots; i++) { Array *rawData = new Array(sizesofslots->get(i)); - readURLData(rawData); + readURLData(fd, rawData); Array *data = stripIVAndDecryptSlot(rawData); slots->set(i, Slot_decode(table, data, mac)); } return slots; } -Array *CloudComm::sendLocalData(Array *sendData, int64_t localSequenceNumber, String host, int port) { +Array *CloudComm::sendLocalData(Array *sendData, int64_t localSequenceNumber, IoTString * host, int port) { if (salt == NULL) return NULL; try { @@ -395,25 +413,21 @@ Array *CloudComm::sendLocalData(Array *sendData, int64_t localSequen Array *encryptedData = encryptSlotAndPrependIV(totalData, iv); // Open a TCP socket connection to a local device - Socket *socket = new Socket(host, port); - socket->setReuseAddress(true); - DataOutputStream *output = new DataOutputStream(socket->getOutputStream()); - DataInputStream *input = new DataInputStream(socket->getInputStream()); + int socket = createSocket(host, port); timer->startTime(); // Send data to output (length of data, the data) - output->writeInt(encryptedData->length()); - output->write(encryptedData, 0, encryptedData->length()); - output->flush(); + writeSocketInt(socket, encryptedData->length()); + writeSocketData(socket, encryptedData); - int lengthOfReturnData = input->readInt(); + int lengthOfReturnData = readSocketInt(socket); Array *returnData = new Array(lengthOfReturnData); - input->readFully(returnData); + readSocketData(socket, returnData); timer->endTime(); returnData = stripIVAndDecryptSlot(returnData); // We are done with this socket - socket->close(); + close(socket); mac->update(returnData, 0, returnData->length() - CloudComm_HMAC_SIZE); Array *realmac = mac->doFinal(); Array *recmac = new Array(CloudComm_HMAC_SIZE); @@ -434,13 +448,11 @@ Array *CloudComm::sendLocalData(Array *sendData, int64_t localSequen } void CloudComm::localServerWorkerFunction() { - ServerSocket *inputSocket = NULL; + int inputSocket = -1; try { // Local server socket - inputSocket = new ServerSocket(listeningPort); - inputSocket->setReuseAddress(true); - inputSocket->setSoTimeout(CloudComm_TIMEOUT_MILLIS); + inputSocket = createSocket(listeningPort); } catch (Exception *e) { throw new Error("Local server setup failure..."); } @@ -448,14 +460,12 @@ void CloudComm::localServerWorkerFunction() { while (!doEnd) { try { // Accept incoming socket - Socket *socket = inputSocket->accept(); - DataInputStream *input = new DataInputStream(socket->getInputStream()); - DataOutputStream *output = new DataOutputStream(socket->getOutputStream()); + int socket = acceptSocket(inputSocket); // Get the encrypted data from the server - int dataSize = input->readInt(); + int dataSize = readSocketInt(socket); Array *readData = new Array(dataSize); - input->readFully(readData); + readSocketData(socket, readData); timer->endTime(); // Decrypt the data @@ -485,26 +495,23 @@ void CloudComm::localServerWorkerFunction() { timer->startTime(); // Send data to output (length of data, the data) - output->writeInt(encryptedData->length()); - output->write(encryptedData, 0, encryptedData->length()); - output->flush(); - - // close the socket - socket->close(); + writeSocketInt(socket, encryptedData->length()); + writeSocketData(socket, encryptedData); + close(socket); } catch (Exception *e) { } } - if (inputSocket != NULL) { + if (inputSocket != -1) { try { - inputSocket->close(); + close(inputSocket); } catch (Exception *e) { throw new Error("Local server close failure..."); } } } -void CloudComm::close() { +void CloudComm::closeCloud() { doEnd = true; if (localServerThread != NULL) { diff --git a/version2/src/C/CloudComm.h b/version2/src/C/CloudComm.h index 2f82508..08fba52 100644 --- a/version2/src/C/CloudComm.h +++ b/version2/src/C/CloudComm.h @@ -32,7 +32,9 @@ private: pthread_t localServerThread; bool doEnd; TimingSingleton *timer; - + Array *getslot; + Array *putslot; + /** * Generates Key from password. */ @@ -91,7 +93,7 @@ public: */ Array *sendLocalData(Array *sendData, int64_t localSequenceNumber, IoTString *host, int port); - void close(); + void closeCloud(); void localServerWorkerFunction(); }; #endif diff --git a/version2/src/C/Error.h b/version2/src/C/Error.h index 61f1b6e..4d12c16 100644 --- a/version2/src/C/Error.h +++ b/version2/src/C/Error.h @@ -5,6 +5,11 @@ public: Error(const char *msg) {} }; +class SocketTimeoutException { + public: + private: +}; + class Exception { public: Exception(const char *msg) {} @@ -12,6 +17,7 @@ public: #define ServerException_TypeInputTimeout 1 #define ServerException_TypeConnectTimeout 2 +#define ServerException_TypeSalt 3 class ServerException { public: diff --git a/version2/src/C/Table.cc b/version2/src/C/Table.cc index b43c93d..d3b7e53 100644 --- a/version2/src/C/Table.cc +++ b/version2/src/C/Table.cc @@ -246,7 +246,7 @@ int64_t Table::getArbitrator(IoTString *key) { } void Table::close() { - cloud->close(); + cloud->closeCloud(); } IoTString *Table::getCommitted(IoTString *key) { diff --git a/version2/src/C/array.h b/version2/src/C/array.h index e2e2a67..996a912 100644 --- a/version2/src/C/array.h +++ b/version2/src/C/array.h @@ -19,7 +19,7 @@ public: { } - Array(type *_array, uint _size) : + Array(const type *_array, uint _size) : array((type *) ourmalloc(sizeof(type) * _size)), size(_size) { memcpy(array, _array, _size * sizeof(type)); -- 2.34.1