]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix.c
verify device
[matrix_esp_thesis] / src / matrix.c
index 06409b82ea3f3029d07c8f28dc267b98c900c0bb..18b700a2e412b1d3699b1781bb1a9cb40e7d0930 100644 (file)
 \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
+#define ROOM_SEND_REQUEST_SIZE 256\r
+#define ROOM_SEND_RESPONSE_SIZE 1024\r
+#define ROOM_SEND_URL "/_matrix/client/v3/rooms/%s/send/%s/%d"\r
+\r
+#define ROOMKEY_REQUEST_SIZE (1024*4)\r
 \r
 #define TODEVICE_EVENT_SIZE (1024*5)\r
 #define TODEVICE_URL "/_matrix/client/v3/sendToDevice/%s/%d"\r
@@ -89,8 +91,8 @@ bool JsonSign(
     \r
     int signatureLen = res;\r
     \r
-    char thisSigningKey[DEVICE_KEY_SIZE];\r
-    MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
+    static char thisSigningKey[SIGNING_KEY_SIZE];\r
+    MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, SIGNING_KEY_SIZE);\r
 \r
     static char signatureJson[JSON_SIGNATURE_SIZE];\r
     int signatureJsonLen =\r
@@ -103,7 +105,8 @@ bool JsonSign(
                 "}"\r
             "}",\r
             client->userId,\r
-            "1",\r
+            //"1",\r
+            client->deviceId,\r
             signatureLen, signature);\r
 \r
     struct mjson_fixedbuf result = { sOut, sOutCap, 0 };\r
@@ -257,6 +260,28 @@ MatrixOlmSessionEncrypt(
     return res != olm_error();\r
 }\r
 \r
+bool\r
+MatrixOlmSessionDecrypt(\r
+    MatrixOlmSession * session,\r
+    size_t messageType,\r
+    char * encrypted,\r
+    char * outBuffer, int outBufferCap)\r
+{\r
+    static uint8_t random[OLM_ENCRYPT_RANDOM_SIZE];\r
+    Randomize(random, OLM_ENCRYPT_RANDOM_SIZE);\r
+\r
+    size_t res =\r
+        olm_decrypt(session->session,\r
+            messageType,\r
+            encrypted, strlen(encrypted),\r
+            outBuffer, outBufferCap);\r
+    \r
+    if (res != olm_error() && res < outBufferCap)\r
+        outBuffer[outBufferCap] = '\0';\r
+\r
+    return res != olm_error();\r
+}\r
+\r
 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session\r
 bool\r
 MatrixMegolmOutSessionInit(\r
@@ -510,7 +535,7 @@ MatrixClientUploadOnetimeKeys(
     static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
 \r
     mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
-        "{\"one_time_keys\":{");\r
+        "{");\r
 \r
     static char onetimeKeysBuffer[1024];\r
     olm_account_one_time_keys(client->olmAccount.account,\r
@@ -524,14 +549,15 @@ MatrixClientUploadOnetimeKeys(
     while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) {\r
         static char keyJson[JSON_ONETIME_KEY_SIZE];\r
         \r
-        snprintf(keyJson, JSON_ONETIME_KEY_SIZE,\r
-            "{\"key\":\"%.*s\"}",\r
-            vlen-2, keys + voff+1);\r
+        int keyJsonLen =\r
+            snprintf(keyJson, JSON_ONETIME_KEY_SIZE,\r
+                "{\"key\":\"%.*s\"}",\r
+                vlen-2, keys + voff+1);\r
 \r
         static char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE];\r
 \r
         JsonSign(client,\r
-            keyJson, JSON_ONETIME_KEY_SIZE,\r
+            keyJson, keyJsonLen,\r
             keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);\r
         \r
         mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
@@ -540,16 +566,33 @@ MatrixClientUploadOnetimeKeys(
             keyJsonSigned);\r
     }\r
 \r
-    mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
-        "}}");\r
+    if (requestBuffer[strlen(requestBuffer)-1] == ',')\r
+        requestBuffer[strlen(requestBuffer)-1] = '\0';\r
+\r
+    mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
+        "}");\r
+        \r
+    // static char onetimeKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+    // JsonSign(client,\r
+    //     requestBuffer, strlen(requestBuffer),\r
+    //     onetimeKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
+        \r
+    // static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+    // snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+    // "{\"one_time_keys\":%s}", onetimeKeysSignedBuffer);\r
+    static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+    snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+    "{\"one_time_keys\":%s}", requestBuffer);\r
 \r
     static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
     MatrixHttpPost(client,\r
         KEYS_UPLOAD_URL,\r
-        requestBuffer,\r
+        finalEvent,\r
         responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
         true);\r
 \r
+    printf("%s\n", responseBuffer);\r
+\r
     return true;\r
 }\r
 \r
@@ -565,33 +608,39 @@ MatrixClientUploadDeviceKey(
 \r
     static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
 \r
-    mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
-        "{\"device_keys\":{"\r
-            "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"\r
-            "\"device_id\":\"%s\","\r
-            "\"keys\":{"\r
-                "\"curve25519:%s\":\"%s\","\r
-                "\"ed25519:%s\":\"%s\""\r
-            "},"\r
-            "\"user_id\":\"%s\""\r
-        "}}",\r
-        client->deviceId,\r
-        client->deviceId, thisDeviceKey,\r
-        client->deviceId, thisSigningKey,\r
-        client->userId);\r
+    int deviceKeysBufferLen =\r
+        mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+            "{"\r
+                "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"\r
+                "\"device_id\":\"%s\","\r
+                "\"keys\":{"\r
+                    "\"curve25519:%s\":\"%s\","\r
+                    "\"ed25519:%s\":\"%s\""\r
+                "},"\r
+                "\"user_id\":\"%s\""\r
+            "}",\r
+            client->deviceId,\r
+            client->deviceId, thisDeviceKey,\r
+            client->deviceId, thisSigningKey,\r
+            client->userId);\r
 \r
     static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
     JsonSign(client,\r
-        deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+        deviceKeysBuffer, deviceKeysBufferLen,\r
         deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
-\r
+    \r
+    static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+    snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+    "{\"device_keys\":%s}", deviceKeysSignedBuffer);\r
 \r
     static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
     MatrixHttpPost(client,\r
         KEYS_UPLOAD_URL,\r
-        deviceKeysSignedBuffer,\r
+        finalEvent,\r
         responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
         true);\r
+        \r
+    printf("%s\n", responseBuffer);\r
 \r
     return true;\r
 }\r
@@ -715,14 +764,14 @@ MatrixClientSendEvent(
 {    \r
     static char requestUrl[MAX_URL_LEN];\r
     sprintf(requestUrl,\r
-        ROOMEVENT_URL, roomId, msgType, (int)time(NULL));\r
+        ROOM_SEND_URL, roomId, msgType, (int)time(NULL));\r
 \r
-    static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];\r
+    static char responseBuffer[ROOM_SEND_RESPONSE_SIZE];\r
     bool result =\r
         MatrixHttpPut(client,\r
             requestUrl,\r
             msgBody,\r
-            responseBuffer, ROOMEVENT_RESPONSE_SIZE,\r
+            responseBuffer, ROOM_SEND_RESPONSE_SIZE,\r
             true);\r
     \r
     return result;\r
@@ -738,7 +787,7 @@ MatrixClientSendEventEncrypted(
     const char * msgBody)\r
 {\r
     // event json\r
-    static char requestBuffer[ROOMEVENT_REQUEST_SIZE];\r
+    static char requestBuffer[ROOM_SEND_REQUEST_SIZE];\r
     sprintf(requestBuffer,\r
         "{"\r
         "\"type\":\"%s\","\r
@@ -802,6 +851,27 @@ MatrixClientSync(
             true);\r
 }\r
 \r
+// https://spec.matrix.org/v1.7/client-server-api/#get_matrixclientv3roomsroomideventeventid\r
+bool\r
+MatrixClientGetRoomEvent(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    const char * eventId,\r
+    char * outEvent, int outEventCap)\r
+{\r
+    static char url[MAX_URL_LEN];\r
+    snprintf(url, MAX_URL_LEN,\r
+        "/_matrix/client/v3/rooms/%s/event/%s",\r
+            roomId,\r
+            eventId);\r
+\r
+    return\r
+        MatrixHttpGet(client,\r
+            url,\r
+            outEvent, outEventCap,\r
+            true);\r
+}\r
+\r
 bool\r
 MatrixClientShareMegolmOutSession(\r
     MatrixClient * client,\r
@@ -932,6 +1002,48 @@ MatrixClientInitMegolmOutSession(
     return false;\r
 }\r
 \r
+bool\r
+MatrixClientRequestMegolmInSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    const char * sessionId,\r
+    const char * senderKey,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    MatrixMegolmInSession * outMegolmInSession)\r
+{\r
+    // TODO: cancel requests\r
+    MatrixClientSendDummy(client, userId, deviceId);\r
+\r
+    static char event[ROOMKEY_REQUEST_SIZE];\r
+    snprintf(event, ROOMKEY_REQUEST_SIZE,\r
+        "{"\r
+            "\"action\":\"request\","\r
+            "\"body\":{"\r
+                "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
+                "\"room_id\":\"%s\","\r
+                "\"sender_key\":\"%s\","\r
+                "\"session_id\":\"%s\""\r
+            "},"\r
+            "\"request_id\":\"%d\","\r
+            "\"requesting_device_id\":\"%s\""\r
+        "}",\r
+        roomId,\r
+        senderKey,\r
+        sessionId,\r
+        time(NULL),\r
+        client->deviceId);\r
+\r
+    \r
+    MatrixClientSendToDevice(client,\r
+        userId,\r
+        deviceId,\r
+        event,\r
+        "m.room_key_request");\r
+\r
+    return true;\r
+}\r
+\r
 bool\r
 MatrixClientGetOlmSession(\r
     MatrixClient * client,\r
@@ -1004,14 +1116,16 @@ MatrixClientSendToDevice(
         deviceId,\r
         message);\r
 \r
-    static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];\r
+    static char responseBuffer[ROOM_SEND_RESPONSE_SIZE];\r
     bool result =\r
         MatrixHttpPut(client,\r
             requestUrl,\r
             eventBuffer,\r
-            responseBuffer, ROOMEVENT_RESPONSE_SIZE,\r
+            responseBuffer, ROOM_SEND_RESPONSE_SIZE,\r
             true);\r
     \r
+    printf("%s\n", responseBuffer);\r
+    \r
     return result;\r
 }\r
 \r
@@ -1097,6 +1211,20 @@ MatrixClientSendToDeviceEncrypted(
         "m.room.encrypted");\r
 }\r
 \r
+bool\r
+MatrixClientSendDummy(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId)\r
+{\r
+    return MatrixClientSendToDeviceEncrypted(\r
+        client,\r
+        userId,\r
+        deviceId,\r
+        "{}",\r
+        "m.dummy");\r
+}\r
+\r
 bool\r
 MatrixClientFindDevice(\r
     MatrixClient * client,\r
@@ -1191,6 +1319,8 @@ MatrixClientRequestDeviceKeys(
     if (! requestResult)\r
         return false;\r
 \r
+    printf("keys:\n%s\n", responseBuffer);\r
+\r
     // query for retrieving device keys for user id\r
     static char query[JSON_QUERY_SIZE];\r
     snprintf(query, JSON_QUERY_SIZE,\r