From: Patrick Date: Thu, 20 Jul 2023 21:15:26 +0000 (+0200) Subject: cli send encrypted & manage megolm session, save/load megolm sessions X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/commitdiff_plain/07e667e29883740aa0b82199cf0518a2e2684e26?ds=sidebyside cli send encrypted & manage megolm session, save/load megolm sessions --- diff --git a/examples/Cli.c b/examples/Cli.c index b2fbe45..4a8e571 100644 --- a/examples/Cli.c +++ b/examples/Cli.c @@ -133,8 +133,6 @@ ExecuteCommand( "{\"body\":\"%s\",\"msgtype\":\"m.text\"}", args[1]); - printf("Sending %s to %s\n", body, args[0]); - MatrixClientSendEvent(client, args[0], "m.room.message", @@ -148,6 +146,43 @@ ExecuteCommand( else if (CheckCommand(cmd, "getuserid")) { printf("User ID: %s\n", client->userId); } + else if (CheckCommand(cmd, "sendencrypted")) { + CHECK_ARGS(2, " ") + + static char body[1024]; + snprintf(body, 1024, + "{\"body\":\"%s\",\"msgtype\":\"m.text\"}", + args[1]); + + MatrixClientSendEventEncrypted(client, + args[0], + "m.room.message", + body); + } + else if (CheckCommand(cmd, "sharesession")) { + CHECK_ARGS(2, " ") + + MatrixClientShareMegolmOutSession(&client, + args[0], + args[1], + &client->megolmOutSessions[0]); + } + else if (CheckCommand(cmd, "savesession")) { + CHECK_ARGS(2, " ") + + MatrixMegolmOutSessionSave( + &client->megolmOutSessions[0], + args[0], + args[1]); + } + else if (CheckCommand(cmd, "loadsession")) { + CHECK_ARGS(2, " ") + + MatrixMegolmOutSessionLoad( + &client->megolmOutSessions[0], + args[0], + args[1]); + } #undef CHECK_ARGS } diff --git a/src/matrix.c b/src/matrix.c index 80802c3..fa7303f 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -226,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 @@ -315,14 +376,9 @@ MatrixClientSetAccessToken( MatrixClient * client, const char * accessToken) { - int accessTokenLen = strlen(accessToken); - - if (accessTokenLen > ACCESS_TOKEN_SIZE - 1) - return false; - - for (int i = 0; i < accessTokenLen; i++) + for (int i = 0; i < ACCESS_TOKEN_SIZE-1; i++) client->accessToken[i] = accessToken[i]; - client->accessToken[accessTokenLen] = '\0'; + client->accessToken[ACCESS_TOKEN_SIZE-1] = '\0'; return true; } @@ -332,14 +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[deviceIdLen] = '\0'; + client->deviceId[DEVICE_ID_SIZE-1] = '\0'; return true; } @@ -349,14 +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[userIdLen] = '\0'; + client->userId[USER_ID_SIZE-1] = '\0'; return true; } diff --git a/src/matrix.h b/src/matrix.h index 91ed208..073f610 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -115,13 +115,25 @@ bool MatrixMegolmOutSessionInit( MatrixMegolmOutSession * session, const char * roomId); - + bool MatrixMegolmOutSessionEncrypt( MatrixMegolmOutSession * session, const char * plaintext, char * outBuffer, int outBufferCap); +bool +MatrixMegolmOutSessionSave( + MatrixMegolmOutSession * session, + const char * filename, + const char * key); + +bool +MatrixMegolmOutSessionLoad( + MatrixMegolmOutSession * session, + const char * filename, + const char * key); + // Matrix Client