diff options
| author | Patrick | 2023-07-20 23:15:26 +0200 |
|---|---|---|
| committer | Patrick | 2023-07-20 23:15:26 +0200 |
| commit | 07e667e29883740aa0b82199cf0518a2e2684e26 (patch) | |
| tree | 0dc0ba2a6fceeeaab1147cbf3b20fec0727f281c | |
| parent | 464bfb1912d0806143386f61c33dd45fbafc38e8 (diff) | |
| download | matrix_esp_thesis-07e667e29883740aa0b82199cf0518a2e2684e26.tar.gz matrix_esp_thesis-07e667e29883740aa0b82199cf0518a2e2684e26.zip | |
cli send encrypted & manage megolm session, save/load megolm sessions
| -rw-r--r-- | examples/Cli.c | 39 | ||||
| -rw-r--r-- | src/matrix.c | 88 | ||||
| -rw-r--r-- | src/matrix.h | 14 |
3 files changed, 117 insertions, 24 deletions
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, "<room_id> <message>")
+
+ 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, "<user_id> <device_id>")
+
+ MatrixClientShareMegolmOutSession(&client,
+ args[0],
+ args[1],
+ &client->megolmOutSessions[0]);
+ }
+ else if (CheckCommand(cmd, "savesession")) {
+ CHECK_ARGS(2, "<filename> <key>")
+
+ MatrixMegolmOutSessionSave(
+ &client->megolmOutSessions[0],
+ args[0],
+ args[1]);
+ }
+ else if (CheckCommand(cmd, "loadsession")) {
+ CHECK_ARGS(2, "<filename> <key>")
+
+ 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
|
