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