]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
sync in verify
authorPatrick <patrick.schoenberger@posteo.de>
Wed, 13 Sep 2023 20:01:42 +0000 (22:01 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Wed, 13 Sep 2023 20:01:42 +0000 (22:01 +0200)
examples/Decrypt.c
src/matrix.c
src/matrix.h

index fe305cc6de0788191a6ff868d3de57b96fe94206..ec320f00db6631c0a3eff988ff910d77edbc8f44 100644 (file)
@@ -53,10 +53,10 @@ main(void)
     printf("event: %s\n", eventBuffer);\r
 \r
     // verify\r
-    char theirDeviceKey[DEVICE_KEY_SIZE];\r
-    MatrixClientRequestDeviceKey(&client,\r
-        DEVICE_ID,\r
-        theirDeviceKey, DEVICE_KEY_SIZE);\r
+    // char theirDeviceKey[DEVICE_KEY_SIZE];\r
+    // MatrixClientRequestDeviceKey(&client,\r
+    //     DEVICE_ID,\r
+    //     theirDeviceKey, DEVICE_KEY_SIZE);\r
     \r
     char transactionId[256];\r
     GetLine(transactionId, 128);\r
index 147d9198730c1221a7b0b6db2f44270afefc28eb..40c2e5b2b673c4df2328df903bd02078d61498b4 100644 (file)
 #define KEYS_CLAIM_REQUEST_SIZE 1024\r
 #define KEYS_CLAIM_RESPONSE_SIZE 1024\r
 \r
-#define JSON_QUERY_SIZE 128\r
+#define SYNC_TIMEOUT 5000\r
 \r
+#define JSON_QUERY_SIZE 128\r
+#define JSON_MAX_INDICES 100\r
+#define JSON_MAX_ENTRY_SIZE 1024\r
 \r
+#define MAX(a,b) ((a) > (b) ? (a) : (b))\r
+#define MIN(a,b) ((a) < (b) ? (a) : (b))\r
 \r
 void\r
 Randomize(\r
@@ -78,6 +83,69 @@ JsonEscape(
     return true;\r
 }\r
 \r
+bool\r
+JsonCanonicalize(\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap)\r
+{\r
+    snprintf(sOut, sOutCap, "{}");\r
+\r
+    int koff, klen, voff, vlen, vtype, off;\r
+\r
+    struct Key {\r
+        const char * ptr;\r
+        int len;\r
+    };\r
+\r
+    struct Key keys[JSON_MAX_INDICES];\r
+    int numKeys = 0;\r
+\r
+    for (off = 0; (off = mjson_next(sIn, sInLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0; ) {\r
+        keys[numKeys].ptr = sIn + koff;\r
+        keys[numKeys].len = klen;\r
+        numKeys++;\r
+    }\r
+\r
+    for (int i = 0; i < numKeys; i++) {\r
+        for (int j = i; j < numKeys; j++) {\r
+            if (\r
+                strncmp(\r
+                    keys[i].ptr,\r
+                    keys[j].ptr,\r
+                    MIN(keys[i].len, keys[j].len)\r
+                ) > 0\r
+            ) {\r
+                struct Key k = keys[i];\r
+                keys[i] = keys[j];\r
+                keys[j] = k;\r
+            }\r
+        }\r
+    }\r
+\r
+    for (int i = 0; i < numKeys; i++) {\r
+        char jp[JSON_QUERY_SIZE];\r
+        snprintf(jp, JSON_QUERY_SIZE, "$.%.*s", keys[i].len-2, keys[i].ptr+1);\r
+\r
+        const char * valPtr;\r
+        int valLen;\r
+        mjson_find(sIn, sInLen, jp, &valPtr, &valLen);\r
+        \r
+        static char newEntry[JSON_MAX_ENTRY_SIZE];\r
+        snprintf(newEntry, JSON_MAX_ENTRY_SIZE, "{%.*s:%.*s}", keys[i].len, keys[i].ptr, valLen, valPtr);\r
+\r
+        char * buffer = strdup(sOut);\r
+\r
+        struct mjson_fixedbuf fb = { sOut, sOutCap, 0 };\r
+        mjson_merge(buffer, strlen(buffer), newEntry, strlen(newEntry), mjson_print_fixed_buf, &fb);\r
+\r
+        free(buffer);\r
+    }\r
+\r
+    // TODO: recursively sort entries\r
+\r
+    return true;\r
+}\r
+\r
 bool JsonSign(\r
     MatrixClient * client,\r
     const char * sIn, int sInLen,\r
@@ -184,7 +252,35 @@ MatrixOlmAccountGetSigningKey(
     return true;\r
 }\r
 \r
-// TODO:in/outbound sessions\r
+bool\r
+MatrixOlmSessionFrom(\r
+    MatrixOlmSession * session,\r
+    OlmAccount * olmAccount,\r
+    const char * deviceId,\r
+    const char * deviceKey,\r
+    const char * encrypted)\r
+{\r
+    memset(session, 0, sizeof(MatrixOlmSession));\r
+\r
+    session->deviceId = deviceId;\r
+\r
+    session->session =\r
+        olm_session(session->memory);\r
+    \r
+    char * encryptedCopy = strdup(encrypted);\r
+\r
+    size_t res =\r
+        olm_create_inbound_session_from(session->session, olmAccount,\r
+            deviceKey, strlen(deviceKey),\r
+            encryptedCopy, strlen(encryptedCopy));\r
+    \r
+    if (res == olm_error()) {\r
+        printf("error olm:%s\n", olm_session_last_error(session->session));\r
+    }\r
+\r
+    return res != olm_error();\r
+}\r
+\r
 bool\r
 MatrixOlmSessionTo(\r
     MatrixOlmSession * session,\r
@@ -214,7 +310,7 @@ MatrixOlmSessionTo(
         printf("error olm:%s\n", olm_session_last_error(session->session));\r
     }\r
 \r
-    return session->session != NULL;\r
+    return res != olm_error();\r
 }\r
 \r
 bool\r
@@ -277,11 +373,68 @@ MatrixOlmSessionDecrypt(
             outBuffer, outBufferCap);\r
     \r
     if (res != olm_error() && res < outBufferCap)\r
-        outBuffer[outBufferCap] = '\0';\r
+        outBuffer[res] = '\0';\r
+\r
+    return res != olm_error();\r
+}\r
+\r
+bool\r
+MatrixMegolmInSessionInit(\r
+    MatrixMegolmInSession * session,\r
+    const char * roomId,\r
+    const char * sessionId,\r
+    const char * sessionKey, int sessionKeyLen)\r
+{\r
+    memset(session, 0, sizeof(MatrixMegolmInSession));\r
+    \r
+    strncpy(session->roomId, roomId, sizeof(session->roomId));\r
+    strncpy(session->id, sessionId, sizeof(session->id));\r
+    strncpy(session->key, sessionKey, sizeof(session->key));\r
+\r
+    session->session =\r
+        olm_inbound_group_session(session->memory);\r
+\r
+    size_t res =\r
+        olm_init_inbound_group_session(\r
+        // olm_import_inbound_group_session(\r
+            session->session,\r
+            (const uint8_t *)sessionKey, sessionKeyLen);\r
+    if (res == olm_error()) {\r
+        printf("Error initializing Megolm session: %s\n", olm_inbound_group_session_last_error(session->session));\r
+    }\r
 \r
     return res != olm_error();\r
 }\r
 \r
+bool\r
+MatrixMegolmInSessionDecrypt(\r
+    MatrixMegolmInSession * session,\r
+    const char * encrypted, int encryptedLen,\r
+    char * outDecrypted, int outDecryptedCap)\r
+{\r
+    // uint8_t buffer[1024];\r
+    // memcpy(buffer, encrypted, encryptedLen);\r
+\r
+    uint32_t megolmInMessageIndex;\r
+\r
+    size_t res =\r
+        olm_group_decrypt(session->session,\r
+            (uint8_t *)encrypted, encryptedLen,\r
+            (uint8_t *)outDecrypted, outDecryptedCap,\r
+            &megolmInMessageIndex);\r
+    \r
+    printf("message index: %d\n", megolmInMessageIndex);\r
+    \r
+    if (res == olm_error()) {\r
+        printf("error decrypting megolm message: %s\n", olm_inbound_group_session_last_error(session->session));\r
+    }\r
+    else {\r
+        printf("decrypted len: %d\n", res);\r
+    }\r
+    \r
+    return true;\r
+}\r
+\r
 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session\r
 bool\r
 MatrixMegolmOutSessionInit(\r
@@ -591,8 +744,6 @@ MatrixClientUploadOnetimeKeys(
         responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
         true);\r
 \r
-    printf("%s\n", responseBuffer);\r
-\r
     return true;\r
 }\r
 \r
@@ -639,8 +790,6 @@ MatrixClientUploadDeviceKey(
         finalEvent,\r
         responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
         true);\r
-        \r
-    printf("%s\n", responseBuffer);\r
 \r
     return true;\r
 }\r
@@ -800,7 +949,8 @@ MatrixClientSendEventEncrypted(
 \r
     // get megolm session\r
     MatrixMegolmOutSession * outSession;\r
-    MatrixClientGetMegolmOutSession(client, roomId, &outSession);\r
+    if (! MatrixClientGetMegolmOutSession(client, roomId, &outSession))\r
+        MatrixClientNewMegolmOutSession(client, roomId, &outSession);\r
         \r
     // encrypt\r
     static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];\r
@@ -848,12 +998,13 @@ MatrixClientSync(
     // filter={\"event_fields\":[\"to_device\"]}\r
     static char url[MAX_URL_LEN];\r
     snprintf(url, MAX_URL_LEN,\r
-        "/_matrix/client/v3/sync%s",\r
-        strlen(nextBatch) > 0 ? "?since=" : "");\r
+        "/_matrix/client/v3/sync?timeout=%d%s",\r
+        SYNC_TIMEOUT,\r
+        strlen(nextBatch) > 0 ? "&since=" : "");\r
     \r
     int index = strlen(url);\r
 \r
-    for (int i = 0; i < strlen(nextBatch); i++) {\r
+    for (size_t i = 0; i < strlen(nextBatch); i++) {\r
         char c = nextBatch[i];\r
 \r
         if (c == '~') {\r
@@ -916,16 +1067,6 @@ MatrixClientShareMegolmOutSession(
         session->key\r
     );\r
 \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
-\r
     // send\r
     MatrixClientSendToDeviceEncrypted(client,\r
         userId,\r
@@ -967,23 +1108,6 @@ MatrixClientShareMegolmOutSessionTest(
     return true;\r
 }\r
 \r
-// bool\r
-// MatrixClientSetMegolmOutSession(\r
-//     MatrixClient * client,\r
-//     const char * roomId,\r
-//     MatrixMegolmOutSession session)\r
-// {\r
-//     if (client->numMegolmOutSessions < 10)\r
-//     {\r
-//         session.roomId = roomId;\r
-//         client->megolmOutSessions[client->numMegolmOutSessions] = session;\r
-//         client->numMegolmOutSessions++;\r
-\r
-//         return true;\r
-//     }\r
-//     return false;\r
-// }\r
-\r
 bool\r
 MatrixClientGetMegolmOutSession(\r
     MatrixClient * client,\r
@@ -999,8 +1123,27 @@ MatrixClientGetMegolmOutSession(
         }\r
     }\r
 \r
-    if (MatrixClientInitMegolmOutSession(client, roomId)) {\r
-        *outSession = &client->megolmOutSessions[client->numMegolmOutSessions-1];\r
+    return false;\r
+}\r
+\r
+bool\r
+MatrixClientNewMegolmOutSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    MatrixMegolmOutSession ** outSession)\r
+{\r
+    if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)\r
+    {\r
+        MatrixMegolmOutSession * result =\r
+            &client->megolmOutSessions[client->numMegolmOutSessions];\r
+        \r
+        MatrixMegolmOutSessionInit(result,\r
+            roomId);\r
+\r
+        *outSession = result;\r
+\r
+        client->numMegolmOutSessions++;\r
+\r
         return true;\r
     }\r
 \r
@@ -1008,20 +1151,50 @@ MatrixClientGetMegolmOutSession(
 }\r
 \r
 bool\r
-MatrixClientInitMegolmOutSession(\r
+MatrixClientGetMegolmInSession(\r
     MatrixClient * client,\r
-    const char * roomId)\r
+    const char * roomId, int roomIdLen,\r
+    const char * sessionId, int sessionIdLen,\r
+    MatrixMegolmInSession ** outSession)\r
 {\r
-    if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)\r
+    for (int i = 0; i < client->numMegolmInSessions; i++)\r
     {\r
-        MatrixMegolmOutSessionInit(\r
-            &client->megolmOutSessions[client->numMegolmOutSessions],\r
-            roomId);\r
+        if (strncmp(client->megolmInSessions[i].roomId, roomId, roomIdLen) == 0 &&\r
+            strncmp(client->megolmInSessions[i].id, sessionId, sessionIdLen) == 0)\r
+        {\r
+            *outSession = &client->megolmInSessions[i];\r
+            return true;\r
+        }\r
+    }\r
+\r
+    return false;\r
+}\r
+\r
+bool\r
+MatrixClientNewMegolmInSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    const char * sessionId,\r
+    const char * sessionKey,\r
+    MatrixMegolmInSession ** outSession)\r
+{\r
+    if (client->numMegolmInSessions < NUM_MEGOLM_SESSIONS)\r
+    {\r
+        MatrixMegolmInSession * result =\r
+            &client->megolmInSessions[client->numMegolmInSessions];\r
         \r
-        client->numMegolmOutSessions++;\r
+        MatrixMegolmInSessionInit(result,\r
+            roomId,\r
+            sessionId,\r
+            sessionKey, strlen(sessionKey));\r
+        \r
+        *outSession = result;\r
+\r
+        client->numMegolmInSessions++;\r
 \r
         return true;\r
     }\r
+\r
     return false;\r
 }\r
 \r
@@ -1032,8 +1205,7 @@ MatrixClientRequestMegolmInSession(
     const char * sessionId,\r
     const char * senderKey,\r
     const char * userId,\r
-    const char * deviceId,\r
-    MatrixMegolmInSession * outMegolmInSession)\r
+    const char * deviceId)\r
 {\r
     // TODO: cancel requests\r
     MatrixClientSendDummy(client, userId, deviceId);\r
@@ -1068,7 +1240,48 @@ MatrixClientRequestMegolmInSession(
 }\r
 \r
 bool\r
-MatrixClientGetOlmSession(\r
+MatrixClientGetOlmSessionIn(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    const char * encrypted,\r
+    MatrixOlmSession ** outSession)\r
+{\r
+    for (int i = 0; i < client->numOlmSessions; i++)\r
+    {\r
+        if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)\r
+        {\r
+            *outSession = &client->olmSessions[i];\r
+            return true;\r
+        }\r
+    }\r
+\r
+    if (client->numOlmSessions < NUM_OLM_SESSIONS)\r
+    {\r
+        static char deviceKey[DEVICE_KEY_SIZE];\r
+        MatrixClientRequestDeviceKey(client,\r
+            deviceId,\r
+            deviceKey, DEVICE_KEY_SIZE);\r
+\r
+        MatrixOlmSessionFrom(\r
+            &client->olmSessions[client->numOlmSessions],\r
+            client->olmAccount.account,\r
+            deviceId,\r
+            deviceKey,\r
+            encrypted);\r
+\r
+        *outSession = &client->olmSessions[client->numOlmSessions];\r
+        \r
+        client->numOlmSessions++;\r
+\r
+        return true;\r
+    }\r
+\r
+    return false;\r
+}\r
+\r
+bool\r
+MatrixClientGetOlmSessionOut(\r
     MatrixClient * client,\r
     const char * userId,\r
     const char * deviceId,\r
@@ -1162,7 +1375,7 @@ MatrixClientSendToDeviceEncrypted(
 {\r
     // get olm session\r
     MatrixOlmSession * olmSession;\r
-    MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);\r
+    MatrixClientGetOlmSessionOut(client, userId, deviceId, &olmSession);\r
 \r
     // create event json\r
     char targetDeviceKey[DEVICE_KEY_SIZE];\r
@@ -1193,8 +1406,6 @@ MatrixClientSendToDeviceEncrypted(
         userId, // recipient user id\r
         targetSigningKey, // recipient device key\r
         thisSigningKey);\r
-    \r
-    printf("%s\n", eventBuffer);\r
 \r
     // encrypt\r
     static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];\r
@@ -1342,8 +1553,6 @@ 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
index da0e85930b726169adbcd3fd0dcbe663b58f884f..4037f0968705f717c8b6cbd7f17394672662be4b 100644 (file)
@@ -49,7 +49,7 @@
 \r
 #define NUM_MEGOLM_SESSIONS 10\r
 #define NUM_OLM_SESSIONS 10\r
-#define NUM_DEVICES 10\r
+#define NUM_DEVICES 100\r
 \r
 // Matrix Device\r
 \r
@@ -105,6 +105,14 @@ MatrixOlmSessionUnpickle(
     void * pickled, int pickledLen,\r
     const void * key, int keyLen);\r
 \r
+bool\r
+MatrixOlmSessionFrom(\r
+    MatrixOlmSession * session,\r
+    OlmAccount * olmAccount,\r
+    const char * deviceId,\r
+    const char * deviceKey,\r
+    const char * encrypted);\r
+\r
 bool\r
 MatrixOlmSessionTo(\r
     MatrixOlmSession * session,\r
@@ -130,23 +138,35 @@ MatrixOlmSessionDecrypt(
 // Matrix Megolm Session\r
 \r
 typedef struct MatrixMegolmInSession {\r
+    char roomId[ROOM_ID_SIZE];\r
+    char id[MEGOLM_SESSION_ID_SIZE];\r
+    char key[MEGOLM_SESSION_KEY_SIZE];\r
+\r
     OlmInboundGroupSession * session;\r
+    char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
+\r
 } MatrixMegolmInSession;\r
 \r
+bool\r
+MatrixMegolmInSessionInit(\r
+    MatrixMegolmInSession * session,\r
+    const char * roomId,\r
+    const char * sessionId,\r
+    const char * sessionKey, int sessionKeyLen);\r
+\r
 bool\r
 MatrixMegolmInSessionDecrypt(\r
-    MatrixMegolmInSession * megolmInSession,\r
-    const char * encrypted,\r
+    MatrixMegolmInSession * session,\r
+    const char * encrypted, int encryptedLen,\r
     char * outDecrypted, int outDecryptedCap);\r
 \r
 typedef struct MatrixMegolmOutSession {\r
     char roomId[ROOM_ID_SIZE];\r
+    char id[MEGOLM_SESSION_ID_SIZE];\r
+    char key[MEGOLM_SESSION_KEY_SIZE];\r
 \r
     OlmOutboundGroupSession * session;\r
     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
-\r
-    char id[MEGOLM_SESSION_ID_SIZE];\r
-    char key[MEGOLM_SESSION_KEY_SIZE];\r
 } MatrixMegolmOutSession;\r
 \r
 bool\r
@@ -306,15 +326,25 @@ MatrixClientGetMegolmOutSession(
     MatrixMegolmOutSession ** outSession);\r
 \r
 bool\r
-MatrixClientSetMegolmOutSession(\r
+MatrixClientNewMegolmOutSession(\r
     MatrixClient * client,\r
     const char * roomId,\r
-    MatrixMegolmOutSession session);\r
+    MatrixMegolmOutSession ** outSession);\r
 \r
 bool\r
-MatrixClientInitMegolmOutSession(\r
+MatrixClientGetMegolmInSession(\r
     MatrixClient * client,\r
-    const char * roomId);\r
+    const char * roomId, int roomIdLen,\r
+    const char * sessionId, int sessionIdLen,\r
+    MatrixMegolmInSession ** outSession);\r
+\r
+bool\r
+MatrixClientNewMegolmInSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    const char * sessionId,\r
+    const char * sessionKey,\r
+    MatrixMegolmInSession ** outSession);\r
     \r
 bool\r
 MatrixClientRequestMegolmInSession(\r
@@ -323,11 +353,18 @@ MatrixClientRequestMegolmInSession(
     const char * sessionId,\r
     const char * senderKey,\r
     const char * userId,\r
-    const char * deviceId, // TODO: remove deviceId (query all devices)\r
-    MatrixMegolmInSession * outMegolmInSession);\r
+    const char * deviceId); // TODO: remove deviceId (query all devices)\r
 \r
 bool\r
-MatrixClientGetOlmSession(\r
+MatrixClientGetOlmSessionIn(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    const char * encrypted,\r
+    MatrixOlmSession ** outSession);\r
+    \r
+bool\r
+MatrixClientGetOlmSessionOut(\r
     MatrixClient * client,\r
     const char * userId,\r
     const char * deviceId,\r
@@ -423,6 +460,11 @@ JsonEscape(
     const char * sIn, int sInLen,\r
     char * sOut, int sOutCap);\r
     \r
+bool\r
+JsonCanonicalize(\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap);\r
+    \r
 bool\r
 JsonSign(\r
     MatrixClient * client,\r