X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/f2840d9dd5b8a0683abee189e408c5a6de294eb7..07e667e29883740aa0b82199cf0518a2e2684e26:/src/matrix.c diff --git a/src/matrix.c b/src/matrix.c index 966a038..fa7303f 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; } @@ -221,6 +226,67 @@ MatrixMegolmOutSessionEncrypt( return res != olm_error(); } +bool +MatrixMegolmOutSessionSave( + MatrixMegolmOutSession * session, + const char * filename, + const char * key) +{ + FILE * f = fopen(filename, "w"); + + size_t roomIdLen = strlen(session->roomId); + fwrite(&roomIdLen, sizeof(size_t), 1, f); + fwrite(session->roomId, 1, roomIdLen, f); + + size_t pickleBufferLen = + olm_pickle_outbound_group_session_length( + session->session); + void * pickleBuffer = malloc(pickleBufferLen); + + olm_pickle_outbound_group_session( + session->session, + key, strlen(key), + pickleBuffer, pickleBufferLen); + + fwrite(&pickleBufferLen, sizeof(size_t), 1, f); + fwrite(pickleBuffer, 1, pickleBufferLen, f); + free(pickleBuffer); + + fclose(f); + + return true; +} + +bool +MatrixMegolmOutSessionLoad( + MatrixMegolmOutSession * session, + const char * filename, + const char * key) +{ + FILE * f = fopen(filename, "r"); + + size_t roomIdLen; + fread(&roomIdLen, sizeof(size_t), 1, f); + fread(session->roomId, 1, roomIdLen, f); + + size_t pickleBufferLen; + fread(&pickleBufferLen, sizeof(size_t), 1, f); + + void * pickleBuffer = malloc(pickleBufferLen); + fread(pickleBuffer, 1, pickleBufferLen, f); + + olm_unpickle_outbound_group_session( + session->session, + key, strlen(key), + pickleBuffer, pickleBufferLen); + + free(pickleBuffer); + + fclose(f); + + return true; +} + bool @@ -254,17 +320,65 @@ MatrixClientInit( } bool -MatrixClientSetAccessToken( +MatrixClientSave( MatrixClient * client, - const char * accessToken) + const char * filename) { - int accessTokenLen = strlen(accessToken); + 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); + } - if (accessTokenLen > ACCESS_TOKEN_SIZE - 1) - return false; + 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; +} - for (int i = 0; i < accessTokenLen; i++) +bool +MatrixClientSetAccessToken( + MatrixClient * client, + const char * accessToken) +{ + for (int i = 0; i < ACCESS_TOKEN_SIZE-1; i++) client->accessToken[i] = accessToken[i]; + client->accessToken[ACCESS_TOKEN_SIZE-1] = '\0'; return true; } @@ -274,13 +388,9 @@ MatrixClientSetDeviceId( MatrixClient * client, const char * deviceId) { - int deviceIdLen = strlen(deviceId); - - if (deviceIdLen > DEVICE_ID_SIZE - 1) - return false; - - for (int i = 0; i < deviceIdLen; i++) + for (int i = 0; i < DEVICE_ID_SIZE-1; i++) client->deviceId[i] = deviceId[i]; + client->deviceId[DEVICE_ID_SIZE-1] = '\0'; return true; } @@ -290,13 +400,9 @@ MatrixClientSetUserId( MatrixClient * client, const char * userId) { - int userIdLen = strlen(userId); - - if (userIdLen > USER_ID_SIZE - 1) - return false; - - for (int i = 0; i < userIdLen; i++) + for (int i = 0; i < USER_ID_SIZE-1; i++) client->userId[i] = userId[i]; + client->userId[USER_ID_SIZE-1] = '\0'; return true; } @@ -455,8 +561,6 @@ MatrixClientClaimOnetimeKey( mjson_get_string(keyObject + voff, vlen, "$.key", outOnetimeKey, outOnetimeKeyCap); - - printf("onetime key: %s\n", outOnetimeKey); // TODO: verify signature @@ -579,15 +683,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 +734,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 +861,7 @@ MatrixClientGetOlmSession( deviceId, onetimeKey, ONETIME_KEY_SIZE); - MatrixOlmSessionFrom( + MatrixOlmSessionTo( &client->olmSessions[client->numOlmSessions], client->olmAccount.account, deviceId, @@ -826,7 +930,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 +961,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 +1013,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);