*/
SecretKeySpec *CloudComm::initKey() {
try {
- PBEKeySpec keyspec = new PBEKeySpec(password->internalBytes(),
- salt,
- 65536,
- 128);
- SecretKey tmpkey = SecretKeyFactory_getInstance("PBKDF2WithHmacSHA256")->generateSecret(keyspec);
+ PBEKeySpec *keyspec = new PBEKeySpec(password->internalBytes(),
+ salt,
+ 65536,
+ 128);
+ SecretKey *tmpkey = SecretKeyFactory_getInstance("PBKDF2WithHmacSHA256")->generateSecret(keyspec);
return new SecretKeySpec(tmpkey->getEncoded(), "AES");
} catch (Exception *e) {
throw new Error("Failed generating key.");
* Inits the HMAC generator.
*/
void CloudComm::initCrypt() {
-
if (password == NULL) {
return;
}
printf("%d\n", (int)saltTmp->get(i) & 255);
}
-
URL *url = new URL(baseurl + "?req=setsalt");
-
timer->startTime();
- URLConnection con = url->openConnection();
- HttpURLConnection http = (HttpURLConnection) con;
+ URLConnection *con = url->openConnection();
+ HttpURLConnection *http = (HttpURLConnection *) con;
http->setRequestMethod("POST");
http->setFixedLengthStreamingMode(saltTmp->length());
http->setDoOutput(true);
http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS);
-
-
http->connect();
OutputStream *os = http->getOutputStream();
os->flush();
int responsecode = http->getResponseCode();
- if (responsecode != HttpURLConnection.HTTP_OK) {
+ if (responsecode != HttpURLConnection_HTTP_OK) {
// TODO: Remove this print
- System.out.println(responsecode);
+ printf("%d\n", responsecode);
throw new Error("Invalid response");
}
timer->endTime();
-
salt = saltTmp;
} catch (Exception *e) {
timer->endTime();
- throw new ServerException("Failed setting salt", ServerException.TypeConnectTimeout);
+ throw new ServerException("Failed setting salt", ServerException_TypeConnectTimeout);
}
}
throw new Error("getSlot failed");
}
try {
-
timer->startTime();
con = url->openConnection();
- http = (HttpURLConnection) con;
+ http = (HttpURLConnection *) con;
http->setRequestMethod("POST");
http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS);
http->setReadTimeout(CloudComm_TIMEOUT_MILLIS);
-
-
http->connect();
timer->endTime();
} catch (SocketTimeoutException *e) {
timer->endTime();
- throw new ServerException("getSalt failed", ServerException.TypeConnectTimeout);
+ throw new ServerException("getSalt failed", ServerException_TypeConnectTimeout);
} catch (Exception *e) {
throw new Error("getSlot failed");
}
try {
-
timer->startTime();
-
- int responsecode = http.getResponseCode();
- if (responsecode != HttpURLConnection.HTTP_OK) {
- // TODO: Remove this print
- // System.out.println(responsecode);
+ int responsecode = http->getResponseCode();
+ if (responsecode != HttpURLConnection_HTTP_OK) {
throw new Error("Invalid response");
}
-
- InputStream is = http->getInputStream();
+ InputStream *is = http->getInputStream();
if (is->available() > 0) {
DataInputStream *dis = new DataInputStream(is);
int salt_length = dis->readInt();
dis->readFully(tmp);
salt = tmp;
timer->endTime();
-
return true;
} else {
timer->endTime();
-
return false;
}
} catch (SocketTimeoutException *e) {
timer->endTime();
-
- throw new ServerException("getSalt failed", ServerException.TypeInputTimeout);
+ throw new ServerException("getSalt failed", ServerException_TypeInputTimeout);
} catch (Exception *e) {
throw new Error("getSlot failed");
}
}
Array<char> *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber) {
- ByteBuffer buffer = ByteBuffer.allocate(CloudComm_IV_SIZE);
+ ByteBuffer *buffer = ByteBuffer_allocate(CloudComm_IV_SIZE);
buffer->putLong(machineId);
int64_t localSequenceNumberShifted = localSequenceNumber << 16;
buffer->putLong(localSequenceNumberShifted);
Array<char> *CloudComm::encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes) {
try {
- IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
- Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
- cipher->init(Cipher.ENCRYPT_MODE, key, ivSpec);
+ IvParameterSpec *ivSpec = new IvParameterSpec(ivBytes);
+ Cipher *cipher = Cipher_getInstance("AES/CTR/NoPadding");
+ cipher->init(Cipher_ENCRYPT_MODE, key, ivSpec);
Array<char> *encryptedBytes = cipher->doFinal(rawData);
Array<char> *chars = new Array<char>(encryptedBytes->length() + CloudComm_IV_SIZE);
- System_arraycopy(ivBytes, 0, chars, 0, ivBytes.length());
- System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes.length);
+ System_arraycopy(ivBytes, 0, chars, 0, ivBytes->length());
+ System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes->length());
return chars;
-
} catch (Exception *e) {
throw new Error("Failed To Encrypt");
}
}
-
Array<char> *CloudComm::stripIVAndDecryptSlot(Array<char> *rawData) {
try {
Array<char> *ivBytes = new Array<char>(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);
-
} catch (Exception *e) {
throw new Error("Failed To Decrypt");
}
}
-
/*
* API for putting a slot into the queue. Returns NULL on success.
* On failure, the server will send slots with newer sequence
try {
if (salt == NULL) {
if (!getSalt()) {
- throw new ServerException("putSlot failed", ServerException.TypeSalt);
+ throw new ServerException("putSlot failed", ServerException_TypeSalt);
}
initCrypt();
}
int64_t sequencenumber = slot->getSequenceNumber();
Array<char> *slotBytes = slot->encode(mac);
-
Array<char> *chars = encryptSlotAndPrependIV(slotBytes, slot->getSlotCryptIV());
-
URL *url = buildRequest(true, sequencenumber, max);
-
timer->startTime();
URLConnection *con = url->openConnection();
HttpURLConnection *http = (HttpURLConnection *) con;
-
http->setRequestMethod("POST");
http->setFixedLengthStreamingMode(chars->length);
http->setDoOutput(true);
http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS);
http->setReadTimeout(CloudComm_TIMEOUT_MILLIS);
http->connect();
-
OutputStream *os = http->getOutputStream();
os->write(chars);
os->flush();
-
timer->endTime();
-
-
} catch (ServerException *e) {
timer->endTime();
-
throw e;
} catch (SocketTimeoutException *e) {
timer->endTime();
-
- throw new ServerException("putSlot failed", ServerException.TypeConnectTimeout);
+ throw new ServerException("putSlot failed", ServerException_TypeConnectTimeout);
} catch (Exception *e) {
throw new Error("putSlot failed");
}
-
-
try {
timer->startTime();
- InputStream is = http->getInputStream();
+ InputStream *is = http->getInputStream();
DataInputStream *dis = new DataInputStream(is);
Array<char> *resptype = new Array<char>(7);
dis->readFully(resptype);
return NULL;
} else
throw new Error("Bad response to putslot");
-
} catch (SocketTimeoutException *e) {
timer->endTime();
throw new ServerException("putSlot failed", ServerException->TypeInputTimeout);
try {
if (salt == NULL) {
if (!getSalt()) {
- throw new ServerException("getSlots failed", ServerException.TypeSalt);
+ throw new ServerException("getSlots failed", ServerException_TypeSalt);
}
initCrypt();
}
URL *url = buildRequest(false, sequencenumber, 0);
timer->startTime();
URLConnection *con = url->openConnection();
- HttpURLConnection *http = (HttpURLConnection) con;
+ HttpURLConnection *http = (HttpURLConnection *) con;
http->setRequestMethod("POST");
http->setConnectTimeout(CloudComm_TIMEOUT_MILLIS);
http->setReadTimeout(CloudComm_TIMEOUT_MILLIS);
-
-
-
http->connect();
timer->endTime();
-
} catch (SocketTimeoutException *e) {
timer->endTime();
-
- throw new ServerException("getSlots failed", ServerException.TypeConnectTimeout);
+ throw new ServerException("getSlots failed", ServerException_TypeConnectTimeout);
} catch (ServerException *e) {
timer->endTime();
}
try {
-
timer->startTime();
InputStream *is = http->getInputStream();
DataInputStream *dis = new DataInputStream(is);
Array<char> *resptype = new Array<char>(7);
-
dis->readFully(resptype);
timer->endTime();
-
- if (!resptype->equals("getslot".getBytes()))
+ if (!resptype->equals("getslot"->getBytes()))
throw new Error("Bad Response: " + new String(resptype));
return processSlots(dis);
} catch (SocketTimeoutException *e) {
timer->endTime();
-
- throw new ServerException("getSlots failed", ServerException.TypeInputTimeout);
+ throw new ServerException("getSlots failed", ServerException_TypeInputTimeout);
} catch (Exception *e) {
throw new Error("getSlots failed");
}
Array<Slot *> *CloudComm::processSlots(DataInputStream *dis) {
int numberofslots = dis->readInt();
Array<int> *sizesofslots = new Array<int>(numberofslots);
-
Array<Slot *> *slots = new Array<Slot *>(numberofslots);
+
for (int i = 0; i < numberofslots; i++)
sizesofslots->set(i, dis->readInt());
-
for (int i = 0; i < numberofslots; i++) {
Array<char> *rawData = new Array<char>(sizesofslots->get(i));
dis->readFully(rawData);
-
Array<char> *data = stripIVAndDecryptSlot(rawData);
slots->set(i, Slot_decode(table, data, mac));
}
}
Array<char> *sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, String host, int port) {
- if (salt == NULL) {
+ if (salt == NULL)
return NULL;
- }
try {
printf("Passing Locally\n");
-
mac->update(sendData);
Array<char> *genmac = mac->doFinal();
Array<char> *totalData = new Array<char>(sendData->length() + genmac->length());
- System_arraycopy(sendData, 0, totalData, 0, sendData.length());
- System - arraycopy(genmac, 0, totalData, sendData.length, genmac->length());
+ System_arraycopy(sendData, 0, totalData, 0, sendData->length());
+ System_arraycopy(genmac, 0, totalData, sendData->length(), genmac->length());
// Encrypt the data for sending
Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
int lengthOfReturnData = input->readInt();
Array<char> *returnData = new Array<char>(lengthOfReturnData);
input->readFully(returnData);
-
timer->endTime();
-
returnData = stripIVAndDecryptSlot(returnData);
// We are done with this socket
socket->close();
-
mac->update(returnData, 0, returnData->length - HMAC_SIZE);
Array<char> *realmac = mac->doFinal();
Array<char> *recmac = new Array<char>(HMAC_SIZE);
System_arraycopy(returnData, returnData->length - realmac->length, recmac, 0, realmac->length);
- if (!Arrays->equals(recmac, realmac))
+ if (!recmac->equals(realmac))
throw new Error("Local Error: Invalid HMAC! Potential Attack!");
Array<char> *returnData2 = new Array<char>(lengthOfReturnData - recmac->length());
try {
// Accept incoming socket
Socket *socket = inputSocket->accept();
-
DataInputStream *input = new DataInputStream(socket->getInputStream());
DataOutputStream *output = new DataOutputStream(socket->getOutputStream());
int dataSize = input->readInt();
Array<char> *readData = new Array<char>(dataSize);
input->readFully(readData);
-
timer->endTime();
// Decrypt the data
readData = stripIVAndDecryptSlot(readData);
-
mac->update(readData, 0, readData->length - HMAC_SIZE);
Array<char> *genmac = mac->doFinal();
Array<char> *recmac = new Array<char>(HMAC_SIZE);
// Process the data
Array<char> *sendData = table->acceptDataFromLocal(returnData);
-
-
mac->update(sendData);
Array<char> *realmac = mac->doFinal();
Array<char> *totalData = new Array<char>(sendData->length() + realmac->length());
Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
-
timer->startTime();
// Send data to output (length of data, the data)
output->writeInt(encryptedData->length());
}
}
}
-
-#include "commit.h"
+#include "Commit.h"
+#include "CommitPart.h"
+#include "ByteBuffer.h"
+#include "KeyValue.h"
Commit::Commit() :
parts(new Hashtable<int32_t, CommitPart *>()),
missingParts(NULL),
fldisComplete(false),
hasLastPart(false),
- keyValueUpdateSet(new HashSet<KeyValue *>()),
+ keyValueUpdateSet(new Hashset<KeyValue *>()),
isDead(false),
sequenceNumber(-1),
machineId(-1),
missingParts(NULL),
fldisComplete(true),
hasLastPart(false),
- keyValueUpdateSet(new HashSet<KeyValue *>()),
+ keyValueUpdateSet(new Hashset<KeyValue *>()),
isDead(false),
sequenceNumber(_sequenceNumber),
machineId(_machineId),
liveKeys(new Hashset<IoTString *>) {
}
-void Commit::addPartDecode(CommitPart newPart) {
-
+void Commit::addPartDecode(CommitPart *newPart) {
if (isDead) {
// If dead then just kill this part and move on
newPart->setDead();
// Set dead the old one since the new one is a rescued version of this part
previoslySeenPart->setDead();
} else if (newPart->isLastPart()) {
- missingParts = new HashSet<int32_t>();
+ missingParts = new Hashset<int32_t>();
hasLastPart = true;
for (int i = 0; i < newPart->getPartNumber(); i++) {
// Make all the parts of this transaction dead
for (int32_t partNumber : parts->keySet()) {
- CommitPart part = parts->get(partNumber);
+ CommitPart *part = parts->get(partNumber);
part->setDead();
}
}
// Copy to a smaller version
Array<char> *partData = new Array<char>(copySize);
- System->arraycopy(charData, currentPosition, partData, 0, copySize);
+ System_arraycopy(charData, currentPosition, partData, 0, copySize);
CommitPart part = new CommitPart(NULL, machineId, sequenceNumber, transactionSequenceNumber, commitPartCount, partData, isLastPart);
parts->put(part->getPartNumber(), part);
// Stitch all the data sections together
for (int i = 0; i < parts->keySet()->size(); i++) {
CommitPart *tp = parts->get(i);
- System->arraycopy(tp->getData(), 0, combinedData, currentPosition, tp->getDataSize());
+ System_arraycopy(tp->getData(), 0, combinedData, currentPosition, tp->getDataSize());
currentPosition += tp->getDataSize();
}
// Decode all the updates key values
for (int i = 0; i < numberOfKVUpdates; i++) {
- KeyValue *kv = (KeyValue *)KeyValue->decode(bbDecode);
+ KeyValue *kv = (KeyValue *)KeyValue_decode(bbDecode);
keyValueUpdateSet->add(kv);
liveKeys->add(kv->getKey());
}
keyValueUpdateSet->addAll(newKVs->values());
liveKeys->addAll(newKVs->keySet());
-
}
-
Commit *Commit_merge(Commit *newer, Commit *older, int64_t newSequenceNumber) {
if (older == NULL) {