X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/10325eaf40c80eb91f1c1eceea880c3abd54efd7..c7aba5979c820958aa08947903afb47ace496a16:/src/matrix.c diff --git a/src/matrix.c b/src/matrix.c index fbcf97e..de936de 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -192,7 +192,7 @@ MatrixMegolmOutSessionInit( static uint8_t random[MEGOLM_INIT_RANDOM_SIZE]; Randomize(random, MEGOLM_INIT_RANDOM_SIZE); - session->roomId = roomId; + strncpy(session->roomId, roomId, ROOM_ID_SIZE); session->session = olm_outbound_group_session(session->memory); @@ -226,6 +226,72 @@ 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); + for (int i = roomIdLen; i < ROOM_ID_SIZE; i++) + session->roomId[i] = '\0'; + + 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); + + olm_outbound_group_session_id(session->session, (uint8_t *)session->id, MEGOLM_SESSION_ID_SIZE); + olm_outbound_group_session_key(session->session, (uint8_t *)session->key, MEGOLM_SESSION_KEY_SIZE); + + fclose(f); + + return true; +} + bool @@ -259,17 +325,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; +} - for (int i = 0; i < accessTokenLen; i++) +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, + 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; } @@ -279,13 +393,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; } @@ -295,13 +405,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; } @@ -715,19 +821,29 @@ MatrixClientGetMegolmOutSession( } } + if (MatrixClientInitMegolmOutSession(client, roomId)) { + *outSession = &client->megolmOutSessions[client->numMegolmOutSessions-1]; + return true; + } + + return false; +} + +bool +MatrixClientInitMegolmOutSession( + MatrixClient * client, + const char * roomId) +{ if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS) { MatrixMegolmOutSessionInit( &client->megolmOutSessions[client->numMegolmOutSessions], roomId); - - *outSession = &client->megolmOutSessions[client->numMegolmOutSessions]; client->numMegolmOutSessions++; return true; } - return false; }