X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/f2840d9dd5b8a0683abee189e408c5a6de294eb7..464bfb1912d0806143386f61c33dd45fbafc38e8:/src/matrix.c diff --git a/src/matrix.c b/src/matrix.c index 966a038..80802c3 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -9,8 +9,8 @@ #define LOGIN_RESPONSE_SIZE 1024 #define LOGIN_URL "/_matrix/client/v3/login" -#define ENCRYPTED_REQUEST_SIZE 512 -#define ENCRYPTED_EVENT_SIZE 1024 +#define ENCRYPTED_REQUEST_SIZE (1024*5) +#define ENCRYPTED_EVENT_SIZE (1024*10) #define ROOMEVENT_REQUEST_SIZE 256 #define ROOMEVENT_RESPONSE_SIZE 1024 #define ROOMEVENT_URL "/_matrix/client/v3/rooms/%s/send/%s/%d" @@ -40,7 +40,7 @@ Randomize( uint8_t * random, int randomLen) { - static bool first = false; + static bool first = true; if (first) { srand(time(0)); first = false; } for (int i = 0; i < randomLen; i++) @@ -133,7 +133,7 @@ MatrixOlmAccountInit( // TODO: in/outbound sessions bool -MatrixOlmSessionFrom( +MatrixOlmSessionTo( MatrixOlmSession * session, OlmAccount * olmAccount, const char * deviceId, @@ -150,11 +150,16 @@ MatrixOlmSessionFrom( static uint8_t random[OLM_OUTBOUND_SESSION_RANDOM_SIZE]; Randomize(random, OLM_OUTBOUND_SESSION_RANDOM_SIZE); - olm_create_outbound_session(session->session, - olmAccount, - deviceKey, strlen(deviceKey), - deviceOnetimeKey, strlen(deviceOnetimeKey), - random, OLM_OUTBOUND_SESSION_RANDOM_SIZE); + size_t res = + olm_create_outbound_session(session->session, + olmAccount, + deviceKey, strlen(deviceKey), + deviceOnetimeKey, strlen(deviceOnetimeKey), + random, OLM_OUTBOUND_SESSION_RANDOM_SIZE); + + if (res == olm_error()) { + printf("error olm: %s\n", olm_account_last_error(olmAccount)); + } return session->session != NULL; } @@ -253,6 +258,58 @@ MatrixClientInit( return true; } +bool +MatrixClientSave( + MatrixClient * client, + const char * filename) +{ + FILE * f = fopen(filename, "w"); + + fwrite(client->deviceKey, 1, DEVICE_KEY_SIZE, f); + fwrite(client->signingKey, 1, DEVICE_KEY_SIZE, f); + fwrite(client->userId, 1, USER_ID_SIZE, f); + fwrite(client->server, 1, SERVER_SIZE, f); + fwrite(client->accessToken, 1, ACCESS_TOKEN_SIZE, f); + fwrite(client->deviceId, 1, DEVICE_ID_SIZE, f); + fwrite(client->expireMs, 1, EXPIRE_MS_SIZE, f); + fwrite(client->refreshToken, 1, REFRESH_TOKEN_SIZE, f); + + fwrite(&client->numDevices, sizeof(int), 1, f); + for (int i = 0; i < client->numDevices; i++) { + fwrite(client->devices[i].deviceId, 1, DEVICE_ID_SIZE, f); + fwrite(client->devices[i].deviceKey, 1, DEVICE_KEY_SIZE, f); + } + + fclose(f); + return true; +} + +bool +MatrixClientLoad( + MatrixClient * client, + const char * filename) +{ + FILE * f = fopen(filename, "r"); + + fread(client->deviceKey, 1, DEVICE_KEY_SIZE, f); + fread(client->signingKey, 1, DEVICE_KEY_SIZE, f); + fread(client->userId, 1, USER_ID_SIZE, f); + fread(client->server, 1, SERVER_SIZE, f); + fread(client->accessToken, 1, ACCESS_TOKEN_SIZE, f); + fread(client->deviceId, 1, DEVICE_ID_SIZE, f); + fread(client->expireMs, 1, EXPIRE_MS_SIZE, f); + fread(client->refreshToken, 1, REFRESH_TOKEN_SIZE, f); + + fread(&client->numDevices, sizeof(int), 1, f); + for (int i = 0; i < client->numDevices; i++) { + fread(client->devices[i].deviceId, 1, DEVICE_ID_SIZE, f); + fread(client->devices[i].deviceKey, 1, DEVICE_KEY_SIZE, f); + } + + fclose(f); + return true; +} + bool MatrixClientSetAccessToken( MatrixClient * client, @@ -265,6 +322,7 @@ MatrixClientSetAccessToken( for (int i = 0; i < accessTokenLen; i++) client->accessToken[i] = accessToken[i]; + client->accessToken[accessTokenLen] = '\0'; return true; } @@ -281,6 +339,7 @@ MatrixClientSetDeviceId( for (int i = 0; i < deviceIdLen; i++) client->deviceId[i] = deviceId[i]; + client->deviceId[deviceIdLen] = '\0'; return true; } @@ -297,6 +356,7 @@ MatrixClientSetUserId( for (int i = 0; i < userIdLen; i++) client->userId[i] = userId[i]; + client->userId[userIdLen] = '\0'; return true; } @@ -455,8 +515,6 @@ MatrixClientClaimOnetimeKey( mjson_get_string(keyObject + voff, vlen, "$.key", outOnetimeKey, outOnetimeKeyCap); - - printf("onetime key: %s\n", outOnetimeKey); // TODO: verify signature @@ -579,15 +637,15 @@ MatrixClientSendEventEncrypted( sprintf(encryptedEventBuffer, "{" "\"algorithm\":\"m.megolm.v1.aes-sha2\"," - "\"sender_key\":\"%s\"," "\"ciphertext\":\"%s\"," - "\"session_id\":\"%s\"," - "\"device_id\":\"%s\"" + "\"device_id\":\"%s\"," + "\"sender_key\":\"%s\"," + "\"session_id\":\"%s\"" "}", - senderKey, encryptedBuffer, - sessionId, - deviceId); + deviceId, + senderKey, + sessionId); // send return MatrixClientSendEvent(client, @@ -630,21 +688,21 @@ MatrixClientShareMegolmOutSession( session->key ); - // get olm session - MatrixOlmSession * olmSession; - MatrixClientGetOlmSession(client, userId, deviceId, &olmSession); + // // get olm session + // MatrixOlmSession * olmSession; + // MatrixClientGetOlmSession(client, userId, deviceId, &olmSession); - // encrypt - char encryptedBuffer[KEY_SHARE_EVENT_LEN]; - MatrixOlmSessionEncrypt(olmSession, - eventBuffer, - encryptedBuffer, KEY_SHARE_EVENT_LEN); + // // encrypt + // char encryptedBuffer[KEY_SHARE_EVENT_LEN]; + // MatrixOlmSessionEncrypt(olmSession, + // eventBuffer, + // encryptedBuffer, KEY_SHARE_EVENT_LEN); // send MatrixClientSendToDeviceEncrypted(client, - client->userId, + userId, deviceId, - encryptedBuffer, + eventBuffer, "m.room_key"); return true; @@ -757,7 +815,7 @@ MatrixClientGetOlmSession( deviceId, onetimeKey, ONETIME_KEY_SIZE); - MatrixOlmSessionFrom( + MatrixOlmSessionTo( &client->olmSessions[client->numOlmSessions], client->olmAccount.account, deviceId, @@ -826,7 +884,6 @@ MatrixClientSendToDeviceEncrypted( // create event json char deviceKey[DEVICE_KEY_SIZE]; MatrixClientGetDeviceKey(client, deviceId, deviceKey, DEVICE_KEY_SIZE); - const char * senderKey = client->deviceKey; static char eventBuffer[TODEVICE_EVENT_SIZE]; sprintf(eventBuffer, @@ -858,19 +915,21 @@ MatrixClientSendToDeviceEncrypted( static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE]; sprintf(encryptedEventBuffer, "{" - "\"algorithm\":\"m.megolm.v1.aes-sha2\"," - "\"sender_key\":\"%s\"," + "\"algorithm\":\"m.olm.v1.curve25519-aes-sha2\"," "\"ciphertext\":{" "\"%s\":{" "\"body\":\"%s\"," - "\"type\":\"%d\"" + "\"type\":%d" "}" - "}" + "}," + "\"device_id\":\"%s\"," + "\"sender_key\":\"%s\"" "}", - senderKey, deviceKey, encryptedBuffer, - olmSession->type); + 0, //olmSession->type, + client->deviceId, + client->deviceKey); // send return MatrixClientSendToDevice( @@ -908,9 +967,16 @@ MatrixClientGetDeviceKey( const char * deviceId, char * outDeviceKey, int outDeviceKeyCap) { + MatrixDevice * device; + + if (MatrixClientFindDevice(client, deviceId, &device)) + { + strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap); + return true; + } + MatrixClientRequestDeviceKeys(client); - MatrixDevice * device; if (MatrixClientFindDevice(client, deviceId, &device)) { strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);