]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
more changes towards sharing megolm sessions
authorPatrick <patrick.schoenberger@posteo.de>
Mon, 10 Jul 2023 18:57:49 +0000 (20:57 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Mon, 10 Jul 2023 18:57:49 +0000 (20:57 +0200)
examples/Keys.c
examples/SendEncrypted.c
examples/Sync.c
src/matrix.c
src/matrix.h
src/matrix_http_mongoose.c

index f59a1d55067caeba3cf737ebe86e0d2206ece155..f79488b3bb28be003d959e33b5f6b7562874c55b 100644 (file)
@@ -25,6 +25,8 @@ main(void)
     \r
     MatrixClientUploadOnetimeKeys(&client);\r
     MatrixClientUploadDeviceKeys(&client);\r
+\r
+    printf("device key: %s\n", client.deviceKey);\r
         \r
     MatrixHttpDeinit(&client);\r
 \r
index 7e0b1d4b25d23e24f3af40a556ee82a11c3c79d0..0005592693ecf67bb2c4f3ecafad1fa8965ef451 100644 (file)
@@ -23,18 +23,20 @@ main(void)
     MatrixClientSetUserId(&client,\r
         USER_ID);\r
 \r
-    // MatrixMegolmOutSession megolmOutSession;\r
-    // MatrixMegolmOutSessionInit(&megolmOutSession);\r
 \r
-    // MatrixClientSetMegolmOutSession(&client,\r
-    //     ROOM_ID,\r
-    //     megolmOutSession);\r
+    MatrixClientUploadDeviceKeys(&client);\r
 \r
     MatrixClientSendEventEncrypted(&client,\r
         ROOM_ID,\r
         "m.room.message",\r
         "{\"body\":\"Hello\",\"msgtype\":\"m.text\"}");\r
 \r
+    MatrixClientSendToDeviceEncrypted(&client,\r
+        USER_ID,\r
+        "ULZZOKJBYN",\r
+        "{}",\r
+        "m.dummy");\r
+\r
     MatrixClientShareMegolmOutSession(&client,\r
         USER_ID,\r
         "ULZZOKJBYN",\r
index a49cf652256952f521c5732aa884f898d75f051d..e8450564f8584235e0e26de30f0244d54719f4ee 100644 (file)
@@ -3,7 +3,6 @@
 \r
 #define SERVER       "https://matrix.org"\r
 #define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"\r
-#define DEVICE_ID    "MAZNCCZLBR"\r
 \r
 int\r
 main(void)\r
@@ -17,9 +16,9 @@ main(void)
     MatrixClientSetAccessToken(&client,\r
         ACCESS_TOKEN);\r
 \r
-    static char syncBuffer[20000];\r
+    static char syncBuffer[40000];\r
     MatrixClientSync(&client,\r
-        syncBuffer, 20000);\r
+        syncBuffer, 40000);\r
     printf("%s", syncBuffer);\r
         \r
     MatrixHttpDeinit(&client);\r
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 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
@@ -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
@@ -455,8 +460,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 +582,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 +633,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 +760,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 +829,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 +860,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 +912,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
index 5963d7db202ca42e6783835f1c9c3a3179c47841..a06eab9e0edb7c0a510819f3bf4f101ab5019caf 100644 (file)
@@ -93,7 +93,7 @@ typedef struct MatrixOlmSession {
 } MatrixOlmSession;\r
 \r
 bool\r
-MatrixOlmSessionFrom(\r
+MatrixOlmSessionTo(\r
     MatrixOlmSession * session,\r
     OlmAccount * olmAccount,\r
     const char * deviceId,\r
index 17edbc8cdcfabe7b53e453a05b057ae71520429f..0b6c267cd90e769a483f583337b2453b583aeb0e 100644 (file)
@@ -220,20 +220,6 @@ MatrixHttpPut(
     else\r
         authorizationHeader[0] = '\0';\r
 \r
-    printf("PUT %s HTTP/1.0\r\n"\r
-            "Host: %.*s\r\n"\r
-            "%s"\r
-            "Content-Type: application/json\r\n"\r
-            "Content-Length: %d\r\n"\r
-            "\r\n"\r
-            "%s"\r
-            "\r\n",\r
-            url,\r
-            host.len, host.ptr,\r
-            authorizationHeader,\r
-            strlen(requestBuffer),\r
-            requestBuffer);\r
-\r
     mg_printf(conn->connection,\r
             "PUT %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r