if (password == NULL) {
return;
}
-
try {
key = initKey();
password = NULL;// drop password
* Builds the URL for the given request.
*/
URL *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries) {
- IoTString *reqstring = isput ? "req=putslot" : "req=getslot";
- IoTString *urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
+ const char *reqstring = isput ? "req=putslot" : "req=getslot";
+ char * buffer = (char *) malloc(baseurl->length() + 200);
+ memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+ int offset = baseurl->length();
+ offset+=sprintf(&buffer[offset], "?%s&seq=%" PRId64, reqstring, sequencenumber);
if (maxentries != 0)
- urlstr += "&max=" + maxentries;
+ sprintf(&buffer[offset], "&max=%" PRId64, maxentries);
+ IoTString *urlstr = new IoTString(buffer);
+ free(buffer);
return new URL(urlstr);
}
void CloudComm::setSalt() {
-
if (salt != NULL) {
- // Salt already sent to server so dont set it again
+ // Salt already sent to server so don't set it again
return;
}
-
+
try {
Array<char> *saltTmp = new Array<char>(CloudComm_SALT_SIZE);
random->nextBytes(saltTmp);
- for (int i = 0; i < CloudComm_SALT_SIZE; i++) {
- printf("%d\n", (int)saltTmp->get(i) & 255);
- }
-
- URL *url = new URL(baseurl + "?req=setsalt");
+ char * buffer = (char *) malloc(baseurl->length() + 100);
+ memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+ int offset = baseurl->length();
+ offset+=sprintf(&buffer[offset], "?req=setsalt");
+ IoTString *urlstr = new IoTString(buffer);
+ free(buffer);
+
+ URL *url = new URL(urlstr);
timer->startTime();
URLConnection *con = url->openConnection();
HttpURLConnection *http = (HttpURLConnection *) con;
-
+
http->setRequestMethod("POST");
http->setFixedLengthStreamingMode(saltTmp->length());
http->setDoOutput(true);
int responsecode = http->getResponseCode();
if (responsecode != HttpURLConnection_HTTP_OK) {
- // TODO: Remove this print
- printf("%d\n", responsecode);
throw new Error("Invalid response");
}
HttpURLConnection *http = NULL;
try {
- url = new URL(baseurl + "?req=getsalt");
+ char * buffer = (char *) malloc(baseurl->length() + 100);
+ memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+ int offset = baseurl->length();
+ offset+=sprintf(&buffer[offset], "?req=getsalt");
+ IoTString *urlstr = new IoTString(buffer);
+ free(buffer);
+
+ url = new URL(urlstr);
} catch (Exception *e) {
throw new Error("getSlot failed");
}
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());
Array<char> *encryptedBytes = new Array<char>(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);