]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix.c
more changes towards sharing megolm sessions
[matrix_esp_thesis] / src / matrix.c
index 966a0389c23cc8b39228a5d00aee2c5eab08cf55..1d2c3fac92afe864663542383e212fe2eeaa445d 100644 (file)
@@ -9,7 +9,7 @@
 #define LOGIN_RESPONSE_SIZE 1024\r
 #define LOGIN_URL "/_matrix/client/v3/login"\r
 \r
 #define LOGIN_RESPONSE_SIZE 1024\r
 #define LOGIN_URL "/_matrix/client/v3/login"\r
 \r
-#define ENCRYPTED_REQUEST_SIZE 512\r
+#define ENCRYPTED_REQUEST_SIZE (1024*5)\r
 #define ENCRYPTED_EVENT_SIZE 1024\r
 #define ROOMEVENT_REQUEST_SIZE 256\r
 #define ROOMEVENT_RESPONSE_SIZE 1024\r
 #define ENCRYPTED_EVENT_SIZE 1024\r
 #define ROOMEVENT_REQUEST_SIZE 256\r
 #define ROOMEVENT_RESPONSE_SIZE 1024\r
@@ -40,7 +40,7 @@ Randomize(
     uint8_t * random,\r
     int randomLen)\r
 {\r
     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
     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
 \r
 // TODO: in/outbound sessions\r
 bool\r
-MatrixOlmSessionFrom(\r
+MatrixOlmSessionTo(\r
     MatrixOlmSession * session,\r
     OlmAccount * olmAccount,\r
     const char * deviceId,\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
     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
 \r
     return session->session != NULL;\r
 }\r
@@ -455,8 +460,6 @@ MatrixClientClaimOnetimeKey(
     \r
     mjson_get_string(keyObject + voff, vlen,\r
         "$.key", outOnetimeKey, outOnetimeKeyCap);\r
     \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
     \r
     // TODO: verify signature\r
     \r
@@ -579,15 +582,15 @@ MatrixClientSendEventEncrypted(
     sprintf(encryptedEventBuffer,\r
         "{"\r
         "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
     sprintf(encryptedEventBuffer,\r
         "{"\r
         "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
-        "\"sender_key\":\"%s\","\r
         "\"ciphertext\":\"%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
         "}",\r
-        senderKey,\r
         encryptedBuffer,\r
         encryptedBuffer,\r
-        sessionId,\r
-        deviceId);\r
+        deviceId,\r
+        senderKey,\r
+        sessionId);\r
 \r
     // send\r
     return MatrixClientSendEvent(client,\r
 \r
     // send\r
     return MatrixClientSendEvent(client,\r
@@ -630,21 +633,21 @@ MatrixClientShareMegolmOutSession(
         session->key\r
     );\r
 \r
         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
 \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
 \r
     // send\r
     MatrixClientSendToDeviceEncrypted(client,\r
-        client->userId,\r
+        userId,\r
         deviceId,\r
         deviceId,\r
-        encryptedBuffer,\r
+        eventBuffer,\r
         "m.room_key");\r
 \r
     return true;\r
         "m.room_key");\r
 \r
     return true;\r
@@ -757,7 +760,7 @@ MatrixClientGetOlmSession(
             deviceId,\r
             onetimeKey, ONETIME_KEY_SIZE);\r
 \r
             deviceId,\r
             onetimeKey, ONETIME_KEY_SIZE);\r
 \r
-        MatrixOlmSessionFrom(\r
+        MatrixOlmSessionTo(\r
             &client->olmSessions[client->numOlmSessions],\r
             client->olmAccount.account,\r
             deviceId,\r
             &client->olmSessions[client->numOlmSessions],\r
             client->olmAccount.account,\r
             deviceId,\r
@@ -826,7 +829,6 @@ MatrixClientSendToDeviceEncrypted(
     // create event json\r
     char deviceKey[DEVICE_KEY_SIZE];\r
     MatrixClientGetDeviceKey(client, deviceId, deviceKey, DEVICE_KEY_SIZE);\r
     // 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
     \r
     static char eventBuffer[TODEVICE_EVENT_SIZE];\r
     sprintf(eventBuffer,\r
@@ -858,19 +860,21 @@ MatrixClientSendToDeviceEncrypted(
     static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];\r
     sprintf(encryptedEventBuffer,\r
         "{"\r
     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
         "\"ciphertext\":{"\r
           "\"%s\":{"\r
             "\"body\":\"%s\","\r
-            "\"type\":\"%d\""\r
+            "\"type\":%d"\r
           "}"\r
           "}"\r
-        "}"\r
+        "},"\r
+        "\"device_id\":\"%s\","\r
+        "\"sender_key\":\"%s\""\r
         "}",\r
         "}",\r
-        senderKey,\r
         deviceKey,\r
         encryptedBuffer,\r
         deviceKey,\r
         encryptedBuffer,\r
-        olmSession->type);\r
+        0, //olmSession->type,\r
+        client->deviceId,\r
+        client->deviceKey);\r
 \r
     // send\r
     return MatrixClientSendToDevice(\r
 \r
     // send\r
     return MatrixClientSendToDevice(\r
@@ -908,9 +912,16 @@ MatrixClientGetDeviceKey(
     const char * deviceId,\r
     char * outDeviceKey, int outDeviceKeyCap)\r
 {\r
     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
     MatrixClientRequestDeviceKeys(client);\r
     \r
-    MatrixDevice * device;\r
     if (MatrixClientFindDevice(client, deviceId, &device))\r
     {\r
         strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);\r
     if (MatrixClientFindDevice(client, deviceId, &device))\r
     {\r
         strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);\r