]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
clean up
authorPatrick <patrick.schoenberger@posteo.de>
Tue, 14 Nov 2023 11:35:01 +0000 (12:35 +0100)
committerPatrick <patrick.schoenberger@posteo.de>
Tue, 14 Nov 2023 11:35:01 +0000 (12:35 +0100)
src/matrix.c
src/matrix.h
src/matrix_http_esp32.c
src/matrix_http_mongoose.c

index 4ce78ce6248e12b2e370b13cddf9f893795abe6a..8b186caa945e27cf1f18889d8c296d6d9b4b8e95 100644 (file)
@@ -9,8 +9,10 @@
 #include <esp_random.h>\r
 #endif\r
 \r
+// can be used to disable static allocation\r
 #define STATIC static\r
 \r
+// DEFINES\r
 #define LOGIN_REQUEST_SIZE 1024\r
 #define LOGIN_RESPONSE_SIZE 1024\r
 #define LOGIN_URL "/_matrix/client/v3/login"\r
@@ -53,6 +55,8 @@ STATIC char g_KeysUploadRequestSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];
 #define MAX(a,b) ((a) > (b) ? (a) : (b))\r
 #define MIN(a,b) ((a) < (b) ? (a) : (b))\r
 \r
+// Util\r
+\r
 void\r
 Randomize(\r
     uint8_t * random,\r
@@ -209,7 +213,6 @@ bool JsonSign(
     return true;\r
 }\r
 \r
-\r
 bool\r
 MatrixOlmAccountInit(\r
     MatrixOlmAccount * account)\r
@@ -446,14 +449,9 @@ MatrixMegolmInSessionDecrypt(
             (uint8_t *)outDecrypted, outDecryptedCap,\r
             &megolmInMessageIndex);\r
     \r
-    printf("message index: %d\n", (int)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
@@ -568,7 +566,7 @@ MatrixClientGenerateOnetimeKeys(
     return res != olm_error();\r
 }\r
 \r
-// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
+// https://spec.matrix.org/v1.8/client-server-api/#post_matrixclientv3keysupload\r
 bool\r
 MatrixClientUploadOnetimeKeys(\r
     MatrixClient * client)\r
@@ -580,10 +578,14 @@ MatrixClientUploadOnetimeKeys(
     olm_account_one_time_keys(client->olmAccount.account,\r
         onetimeKeysBuffer, 1024);\r
 \r
+    // olm_account_one_time_keys returns a json object\r
+    // find curve25519 member\r
     const char *keys;\r
     int keysLen;\r
     mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen);\r
 \r
+    // iterate over onetime keys, create key object\r
+    // sign it and append to request\r
     int koff, klen, voff, vlen, vtype, off = 0;\r
     while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) {\r
         STATIC char keyJson[JSON_ONETIME_KEY_SIZE];\r
@@ -605,20 +607,13 @@ MatrixClientUploadOnetimeKeys(
             keyJsonSigned);\r
     }\r
 \r
+    // delete last ',' since the loop always appends it\r
     if (g_KeysUploadRequestBuffer[strlen(g_KeysUploadRequestBuffer)-1] == ',')\r
         g_KeysUploadRequestBuffer[strlen(g_KeysUploadRequestBuffer)-1] = '\0';\r
 \r
     mjson_snprintf(g_KeysUploadRequestBuffer+strlen(g_KeysUploadRequestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(g_KeysUploadRequestBuffer),\r
         "}");\r
-        \r
-    // STATIC char onetimeKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
-    // JsonSign(client,\r
-    //     g_KeysUploadRequestBuffer, strlen(g_KeysUploadRequestBuffer),\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
+\r
     snprintf(g_KeysUploadRequestSignedBuffer, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
     "{\"one_time_keys\":%s}", g_KeysUploadRequestBuffer);\r
 \r
@@ -632,7 +627,7 @@ MatrixClientUploadOnetimeKeys(
     return true;\r
 }\r
 \r
-// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
+// https://spec.matrix.org/v1.8/client-server-api/#post_matrixclientv3keysupload\r
 bool\r
 MatrixClientUploadDeviceKeys(\r
     MatrixClient * client)\r
@@ -676,7 +671,7 @@ MatrixClientUploadDeviceKeys(
     return true;\r
 }\r
 \r
-// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysclaim\r
+// https://spec.matrix.org/v1.8/client-server-api/#post_matrixclientv3keysclaim\r
 bool\r
 MatrixClientClaimOnetimeKey(\r
     MatrixClient * client,\r
@@ -708,18 +703,24 @@ MatrixClientClaimOnetimeKey(
     JsonEscape(userId, strlen(userId),\r
         userIdEscaped, USER_ID_SIZE);\r
     \r
+    // create the json query according to\r
+    // https://spec.matrix.org/v1.8/client-server-api/#post_matrixclientv3keysclaim\r
     STATIC char query[JSON_QUERY_SIZE];\r
     snprintf(query, JSON_QUERY_SIZE,\r
         "$.one_time_keys.%s.%s",\r
         userIdEscaped,\r
         deviceId);\r
     \r
+    // find the corresponding json object\r
     const char * keyObject;\r
     int keyObjectSize;\r
     mjson_find(responseBuffer, strlen(responseBuffer),\r
         query,\r
         &keyObject, &keyObjectSize);\r
     \r
+    // use mjson_next (which iterates over all key/value pairs) once\r
+    // because we only request one key\r
+    // (see https://github.com/cesanta/mjson#mjson_next for details)\r
     int koff, klen, voff, vlen, vtype;\r
     mjson_next(keyObject, keyObjectSize, 0,\r
         &koff, &klen, &voff, &vlen, &vtype);\r
@@ -727,12 +728,12 @@ MatrixClientClaimOnetimeKey(
     mjson_get_string(keyObject + voff, vlen,\r
         "$.key", outOnetimeKey, outOnetimeKeyCap);\r
     \r
-    // TODO:verify signature\r
+    // TODO: verify signature\r
     \r
     return true;\r
 }\r
 \r
-// https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login\r
+// https://spec.matrix.org/v1.8/client-server-api/#post_matrixclientv3login\r
 bool\r
 MatrixClientLoginPassword(\r
     MatrixClient * client,\r
@@ -769,6 +770,7 @@ MatrixClientLoginPassword(
     \r
     int responseLen = strlen(responseBuffer);\r
 \r
+    // store variables in MatrixClient\r
     mjson_get_string(responseBuffer, responseLen,\r
         "$.access_token",\r
         client->accessToken, ACCESS_TOKEN_SIZE);\r
@@ -787,7 +789,7 @@ MatrixClientLoginPassword(
     return true;\r
 }\r
 \r
-// https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid\r
+// https://spec.matrix.org/v1.8/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid\r
 bool\r
 MatrixClientSendEvent(\r
     MatrixClient * client,\r
@@ -796,7 +798,7 @@ MatrixClientSendEvent(
     const char * msgBody)\r
 {    \r
     STATIC char requestUrl[MAX_URL_LEN];\r
-    sprintf(requestUrl,\r
+    snprintf(requestUrl, MAX_URL_LEN,\r
         ROOM_SEND_URL, roomId, msgType, (int)time(NULL));\r
 \r
     STATIC char responseBuffer[ROOM_SEND_RESPONSE_SIZE];\r
@@ -810,7 +812,7 @@ MatrixClientSendEvent(
     return result;\r
 }\r
 \r
-// https://spec.matrix.org/v1.6/client-server-api/#mroomencrypted\r
+// https://spec.matrix.org/v1.8/client-server-api/#mroomencrypted\r
 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#sending-an-encrypted-message-event\r
 bool\r
 MatrixClientSendEventEncrypted(\r
@@ -821,7 +823,7 @@ MatrixClientSendEventEncrypted(
 {\r
     // event json\r
     STATIC char requestBuffer[ROOM_SEND_REQUEST_SIZE];\r
-    sprintf(requestBuffer,\r
+    snprintf(requestBuffer, ROOM_SEND_REQUEST_SIZE,\r
         "{"\r
         "\"type\":\"%s\","\r
         "\"content\":%s,"\r
@@ -870,21 +872,30 @@ MatrixClientSendEventEncrypted(
         g_EncryptedEventBuffer);\r
 }\r
 \r
+// this handles to_device events received from a sync\r
+// mainly for verification (m.key.verification.* events)\r
 void\r
 MatrixClientHandleEvent(\r
     MatrixClient * client,\r
     const char * event, int eventLen\r
 ) {\r
+    // get the event type\r
     STATIC char eventType[128];\r
     memset(eventType, 0, sizeof(eventType));\r
     mjson_get_string(event, eventLen, "$.type", eventType, 128);\r
 \r
+    // static variables for verification\r
+    // since verification takes multiple requests\r
+    // data is cleared when verification is finished or started\r
     static char transactionId[64];\r
     static char verifyFromDeviceId[DEVICE_ID_SIZE];\r
     static OlmSAS * olmSas = NULL;\r
 \r
+    // initial verification request, reply that we are ready to verify\r
     if (strcmp(eventType, "m.key.verification.request") == 0) {\r
+        // reset static data\r
         memset(transactionId, 0, 64);\r
+        memset(verifyFromDeviceId, 0, DEVICE_ID_SIZE);\r
         if (olmSas != NULL)\r
             free(olmSas);\r
         \r
@@ -916,6 +927,8 @@ MatrixClientHandleEvent(
         \r
         OlmUtility * olmUtil = olm_utility(malloc(olm_utility_size()));\r
         \r
+        // calculate commitment according to \r
+        // https://spec.matrix.org/v1.8/client-server-api/#mkeyverificationaccept\r
         STATIC char publicKey[64];\r
         STATIC char keyStartJsonCanonical[512];\r
         STATIC char concat[512+64];\r
@@ -923,20 +936,19 @@ MatrixClientHandleEvent(
         olm_sas_get_pubkey(olmSas,\r
             publicKey,\r
             64);\r
-        printf("public key: %.*s\n", olm_sas_pubkey_length(olmSas), publicKey);\r
 \r
         const char * keyStartJson;\r
         int keyStartJsonLen;\r
         mjson_find(event, eventLen, "$.content", &keyStartJson, &keyStartJsonLen);\r
         JsonCanonicalize(keyStartJson, keyStartJsonLen, keyStartJsonCanonical, 512);\r
 \r
-        printf("json:\n%.*s\ncanonical json:\n%s\n", keyStartJsonLen, keyStartJson, keyStartJsonCanonical);\r
-\r
         int concatLen =\r
-            snprintf(concat, 512+64, "%.*s%s", olm_sas_pubkey_length(olmSas), publicKey, keyStartJsonCanonical);\r
+            snprintf(concat, 512+64, "%.*s%s", (int)olm_sas_pubkey_length(olmSas), publicKey, keyStartJsonCanonical);\r
 \r
         int commitmentLen =\r
             olm_sha256(olmUtil, concat, concatLen, commitment, 1024);\r
+        olm_clear_utility(olmUtil);\r
+        free(olmUtil);\r
         \r
         STATIC char verificationAcceptBuffer[512];\r
         snprintf(verificationAcceptBuffer, 512,\r
@@ -958,6 +970,7 @@ MatrixClientHandleEvent(
             verificationAcceptBuffer,\r
             "m.key.verification.accept");\r
     }\r
+    // send our sas key and calculate sas using their received key\r
     else if (strcmp(eventType, "m.key.verification.key") == 0) {\r
         STATIC char publicKey[128];\r
         olm_sas_get_pubkey(olmSas,\r
@@ -967,10 +980,6 @@ MatrixClientHandleEvent(
         STATIC char theirPublicKey[128];\r
         int theirPublicKeyLen =\r
             mjson_get_string(event, eventLen, "$.content.key", theirPublicKey, 128);\r
-        \r
-        printf("event: %.*s\n", eventLen, event);\r
-        printf("theirPublicKey: %.*s\n", theirPublicKeyLen, theirPublicKey);\r
-        printf("publicKey: %.*s\n", olm_sas_pubkey_length(olmSas), publicKey);\r
 \r
         olm_sas_set_their_key(olmSas, theirPublicKey, theirPublicKeyLen);\r
         \r
@@ -980,7 +989,7 @@ MatrixClientHandleEvent(
             "\"key\":\"%.*s\","\r
             "\"transaction_id\":\"%s\""\r
             "}",\r
-            olm_sas_pubkey_length(olmSas), publicKey,\r
+            (int)olm_sas_pubkey_length(olmSas), publicKey,\r
             transactionId);\r
         \r
         MatrixClientSendToDevice(client,\r
@@ -1009,19 +1018,15 @@ MatrixClientHandleEvent(
         int b2 = sasBytes[2];\r
         int b3 = sasBytes[3];\r
         int b4 = sasBytes[4];\r
-        \r
-        printf("%d %d %d %d %d\n", b0, b1, b2, b3, b4);\r
 \r
-        // https://spec.matrix.org/v1.7/client-server-api/#sas-method-decimal\r
-        printf("%d | %d | %d\n",\r
-            (b0 << 5 | b1 >> 3) + 1000,\r
-            ((b1 & 0x7) << 10 | b2 << 2 | b3 >> 6) + 1000,\r
-            ((b3 & 0x3F) << 7 | b4 >> 1) + 1000);\r
+        // for now just printf SAS numbers\r
+        // https://spec.matrix.org/v1.8/client-server-api/#sas-method-decimal\r
         printf("%d | %d | %d\n",\r
             ((b0 << 5) | (b1 >> 3)) + 1000,\r
             (((b1 & 0x7) << 10) | (b2 << 2) | (b3 >> 6)) + 1000,\r
             (((b3 & 0x3F) << 7) | (b4 >> 1)) + 1000);\r
     }\r
+    // calculate MACs for signing key, master key and the key list\r
     else if (strcmp(eventType, "m.key.verification.mac") == 0) {        \r
         // mac\r
         STATIC char masterKey[123];\r
@@ -1036,6 +1041,8 @@ MatrixClientHandleEvent(
         STATIC char key2[128];\r
         STATIC char key2Mac[128];\r
 \r
+        // keys have to be sorted so write keys/key IDs into key1/key2\r
+        // depending on lexicographical order\r
         if (strcmp(masterKey, client->deviceId) < 0) {\r
             snprintf(key1Id, 1024, "ed25519:%s", masterKey);\r
             strcpy(key1, masterKey);\r
@@ -1049,9 +1056,12 @@ MatrixClientHandleEvent(
             strcpy(key2, masterKey);\r
         }\r
 \r
+        // create key list\r
         snprintf(keyList, 1024,\r
             "%s,%s", key1Id, key2Id);\r
         \r
+        // generate MAC info for both keys and key list\r
+        // https://spec.matrix.org/v1.8/client-server-api/#mac-calculation\r
         STATIC char macInfo[1024];\r
         int macInfoLen;\r
         {\r
@@ -1091,6 +1101,7 @@ MatrixClientHandleEvent(
             olm_sas_calculate_mac_fixed_base64(olmSas, key2, strlen(key2), macInfo, macInfoLen, key2Mac, 1024);\r
         }\r
 \r
+        // construct message and send\r
         STATIC char verificationMacBuffer[1024];\r
         snprintf(verificationMacBuffer, 1024,\r
             "{"\r
@@ -1114,6 +1125,7 @@ MatrixClientHandleEvent(
             verificationMacBuffer,\r
             "m.key.verification.mac");\r
 \r
+        // send 'done' message\r
         STATIC char verificationDoneBuffer[128];\r
         snprintf(verificationDoneBuffer, 128,\r
             "{"\r
@@ -1128,12 +1140,14 @@ MatrixClientHandleEvent(
             "m.key.verification.done");\r
         \r
         free(olmSas);\r
+\r
         client->verified = true;\r
     }\r
     else if (strcmp(eventType, "m.room.encrypted") == 0) {\r
         STATIC char algorithm[128];\r
         mjson_get_string(event, eventLen, "$.content.algorithm", algorithm, 128);\r
 \r
+        // since this only handles to_device messages algorithm should always be olm\r
         if (strcmp(algorithm, "m.olm.v1.curve25519-aes-sha2") == 0) {\r
             STATIC char thisDeviceKey[DEVICE_KEY_SIZE];\r
             MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
@@ -1151,16 +1165,19 @@ MatrixClientHandleEvent(
 \r
             MatrixOlmSession * olmSession;\r
             \r
-            if (! MatrixClientGetOlmSession(client, client->userId, verifyFromDeviceId, &olmSession))\r
-            {\r
-                if (messageTypeInt == 0) {\r
-                    MatrixClientNewOlmSessionIn(client,\r
-                        client->userId,\r
-                        verifyFromDeviceId,\r
-                        g_EncryptedEventBuffer,\r
-                        &olmSession);\r
-                }\r
-                else {\r
+            // depending on message type create new incoming\r
+            // (type 0 indicates a new session so we dont check locally)\r
+            if (messageTypeInt == 0) {\r
+                MatrixClientNewOlmSessionIn(client,\r
+                    client->userId,\r
+                    verifyFromDeviceId,\r
+                    g_EncryptedEventBuffer,\r
+                    &olmSession);\r
+            }\r
+            // or new outgoing, checking for known sessions first\r
+            else {\r
+                if (! MatrixClientGetOlmSession(client, client->userId, verifyFromDeviceId, &olmSession))\r
+                {\r
                     MatrixClientNewOlmSessionOut(client,\r
                         client->userId,\r
                         verifyFromDeviceId,\r
@@ -1177,15 +1194,13 @@ MatrixClientHandleEvent(
     }\r
     else if (strcmp(eventType, "m.room_key") == 0 ||\r
              strcmp(eventType, "m.forwarded_room_key") == 0) {\r
+        // store session information\r
         STATIC char roomId[128];\r
         STATIC char sessionId[128];\r
         STATIC char sessionKey[1024];\r
         mjson_get_string(event, eventLen, "$.content.room_id", roomId, 128);\r
         mjson_get_string(event, eventLen, "$.content.session_id", sessionId, 128);\r
         mjson_get_string(event, eventLen, "$.content.session_key", sessionKey, 1024);\r
-        \r
-        printf("sessionId: %s\n", sessionId);\r
-        printf("sessionKey: %s\n", sessionKey);\r
 \r
         MatrixMegolmInSession * megolmInSession;\r
         MatrixClientNewMegolmInSession(client, roomId, sessionId, sessionKey, &megolmInSession);\r
@@ -1206,6 +1221,8 @@ MatrixClientHandleRoomEvent(
         STATIC char algorithm[128];\r
         mjson_get_string(event, eventLen, "$.content.algorithm", algorithm, 128);\r
 \r
+        // only room specific message type is encrypted\r
+        // since this is only room messages, algorithm should always be megolm\r
         if (strcmp(algorithm, "m.megolm.v1.aes-sha2") == 0) {\r
             STATIC char sessionId[128];\r
             int sessionIdLen =\r
@@ -1228,13 +1245,14 @@ MatrixClientHandleRoomEvent(
                 MatrixClientHandleEvent(client, decrypted, strlen(decrypted));\r
             }\r
             else {\r
-                printf("megolm session not known\n");\r
+                printf("error: megolm session not known\n");\r
             }\r
         }\r
     }\r
     MatrixClientHandleEvent(client, event, eventLen);\r
 }\r
 \r
+// pass the response from sync to Handle(Room)Event\r
 void\r
 MatrixClientHandleSync(\r
     MatrixClient * client,\r
@@ -1246,15 +1264,16 @@ MatrixClientHandleSync(
     const char * s = syncBuffer;\r
     int slen = syncBufferLen;\r
 \r
+    // read next_batch\r
     mjson_get_string(s, slen, "$.next_batch", nextBatch, nextBatchCap);\r
 \r
     // to_device\r
-\r
     const char * events;\r
     int eventsLen;\r
     res =\r
         mjson_find(s, slen, "$.to_device.events", &events, &eventsLen);\r
     \r
+    // iterate event and pass to HandleEvent\r
     if (res != MJSON_TOK_INVALID) {\r
         {\r
         int koff, klen, voff, vlen, vtype, off = 0;\r
@@ -1268,14 +1287,13 @@ MatrixClientHandleSync(
     }\r
 \r
     // rooms\r
-    \r
     const char * rooms;\r
     int roomsLen;\r
     res =\r
         mjson_find(s, slen, "$.rooms.join", &rooms, &roomsLen);\r
     \r
     if (res != MJSON_TOK_INVALID) {\r
-        {\r
+        // iterate rooms\r
         int koff, klen, voff, vlen, vtype, off = 0;\r
         for (off = 0; (off = mjson_next(rooms, roomsLen, off, &koff, &klen,\r
                                         &voff, &vlen, &vtype)) != 0; ) {\r
@@ -1288,7 +1306,7 @@ MatrixClientHandleSync(
                 mjson_find(v, vlen, "$.timeline.events", &events, &eventsLen);\r
             \r
             if (res != MJSON_TOK_INVALID) {\r
-                {\r
+                // iterate messages in that room\r
                 int koff2, klen2, voff2, vlen2, vtype2, off2 = 0;\r
                 for (off2 = 0; (off2 = mjson_next(events, eventsLen, off2, &koff2, &klen2,\r
                                                 &voff2, &vlen2, &vtype2)) != 0; ) {\r
@@ -1298,14 +1316,12 @@ MatrixClientHandleSync(
                         k+1, klen-2,\r
                         v2, vlen2);\r
                 }\r
-                }\r
             }\r
         }\r
-        }\r
     }\r
 }\r
 \r
-// https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3sync\r
+// https://spec.matrix.org/v1.8/client-server-api/#get_matrixclientv3sync\r
 bool\r
 MatrixClientSync(\r
     MatrixClient * client,\r
@@ -1323,6 +1339,7 @@ MatrixClientSync(
     \r
     int index = strlen(url);\r
 \r
+    // URL encode next_batch parameter since it can include ~\r
     for (size_t i = 0; i < strlen(nextBatch); i++) {\r
         char c = nextBatch[i];\r
 \r
@@ -1350,7 +1367,7 @@ MatrixClientSync(
     return result;\r
 }\r
 \r
-// https://spec.matrix.org/v1.7/client-server-api/#get_matrixclientv3roomsroomideventeventid\r
+// https://spec.matrix.org/v1.8/client-server-api/#get_matrixclientv3roomsroomideventeventid\r
 bool\r
 MatrixClientGetRoomEvent(\r
     MatrixClient * client,\r
@@ -1380,7 +1397,7 @@ MatrixClientShareMegolmOutSession(
 {\r
     // generate room key event\r
     STATIC char eventBuffer[KEY_SHARE_EVENT_LEN];\r
-    sprintf(eventBuffer,\r
+    snprintf(eventBuffer, KEY_SHARE_EVENT_LEN,\r
         "{"\r
             "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
             "\"room_id\":\"%s\","\r
@@ -1402,37 +1419,6 @@ MatrixClientShareMegolmOutSession(
     return true;\r
 }\r
 \r
-bool\r
-MatrixClientShareMegolmOutSessionTest(\r
-    MatrixClient * client,\r
-    const char * userId,\r
-    const char * deviceId,\r
-    MatrixMegolmOutSession * session)\r
-{\r
-    // generate room key event\r
-    char eventBuffer[KEY_SHARE_EVENT_LEN];\r
-    sprintf(eventBuffer,\r
-        "{"\r
-            "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
-            "\"room_id\":\"%s\","\r
-            "\"session_id\":\"%s\","\r
-            "\"session_key\":\"%s\""\r
-        "}",\r
-        session->roomId,\r
-        session->id,\r
-        session->key\r
-    );\r
-\r
-    // send\r
-    MatrixClientSendToDevice(client,\r
-        userId,\r
-        deviceId,\r
-        eventBuffer,\r
-        "m.room_key");\r
-\r
-    return true;\r
-}\r
-\r
 bool\r
 MatrixClientGetMegolmOutSession(\r
     MatrixClient * client,\r
@@ -1656,7 +1642,7 @@ MatrixClientNewOlmSessionOut(
     return false;\r
 }\r
 \r
-// https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid\r
+// https://spec.matrix.org/v1.8/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid\r
 bool\r
 MatrixClientSendToDevice(\r
     MatrixClient * client,\r
@@ -1666,7 +1652,7 @@ MatrixClientSendToDevice(
     const char * msgType)\r
 {\r
     STATIC char requestUrl[MAX_URL_LEN];\r
-    sprintf(requestUrl,\r
+    snprintf(requestUrl, MAX_URL_LEN,\r
         TODEVICE_URL, msgType, (int)time(NULL));\r
 \r
     snprintf(g_TodeviceEventBuffer, TODEVICE_EVENT_SIZE,\r
@@ -1689,8 +1675,6 @@ MatrixClientSendToDevice(
             responseBuffer, ROOM_SEND_RESPONSE_SIZE,\r
             true);\r
     \r
-    printf("%s\n", responseBuffer);\r
-    \r
     return result;\r
 }\r
 \r
@@ -1872,34 +1856,36 @@ MatrixClientRequestMasterKey(
     char * outMasterKey, int outMasterKeyCap)\r
 {\r
     if (strlen(client->masterKey) > 0) {\r
-        strncpy(outMasterKey, outMasterKeyCap, client->masterKey);\r
+        strncpy(outMasterKey, client->masterKey, outMasterKeyCap);\r
         return true;\r
     }\r
 \r
     MatrixClientRequestDeviceKeys(client);\r
     \r
     if (strlen(client->masterKey) > 0) {\r
-        strncpy(outMasterKey, outMasterKeyCap, client->masterKey);\r
+        strncpy(outMasterKey, client->masterKey, outMasterKeyCap);\r
         return true;\r
     }\r
 \r
     return false;\r
 }\r
 \r
-// https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3keysquery\r
+// https://spec.matrix.org/v1.8/client-server-api/#post_matrixclientv3keysquery\r
 bool\r
 MatrixClientRequestDeviceKeys(\r
     MatrixClient * client)\r
 {\r
     if (client->numDevices >= NUM_DEVICES) {\r
-        printf("Maximum number of devices reached\n");\r
+        printf("error: Maximum number of devices reached\n");\r
         return false;\r
     }\r
 \r
+    // escape userId so we can use it in json queries\r
     STATIC char userIdEscaped[USER_ID_SIZE];\r
     JsonEscape(client->userId, strlen(client->userId),\r
         userIdEscaped, USER_ID_SIZE);\r
 \r
+    // construct and send request\r
     STATIC char request[KEYS_QUERY_REQUEST_SIZE];\r
     snprintf(request, KEYS_QUERY_REQUEST_SIZE,\r
         "{\"device_keys\":{\"%s\":[]}}", client->userId);\r
@@ -1919,6 +1905,7 @@ MatrixClientRequestDeviceKeys(
     const char * s;\r
     int slen;\r
 \r
+    // look for master key\r
     snprintf(query, JSON_QUERY_SIZE,\r
         "$.master_keys.%s.keys", userIdEscaped);\r
     mjson_find(responseBuffer, strlen(responseBuffer),\r
@@ -1929,10 +1916,9 @@ MatrixClientRequestDeviceKeys(
                                     &voff, &vlen, &vtype)) != 0; ) {\r
         snprintf(client->masterKey, MASTER_KEY_SIZE,\r
             "%.*s", vlen-2, s+voff+1);\r
-\r
-        printf("found master key: %s\n", client->masterKey);\r
     }\r
 \r
+    // iterate over returned devices for that userId\r
     snprintf(query, JSON_QUERY_SIZE,\r
         "$.device_keys.%s", userIdEscaped);\r
     \r
@@ -1940,7 +1926,7 @@ MatrixClientRequestDeviceKeys(
         query, &s, &slen);\r
     \r
     // loop over keys\r
-    \r
+    // creating a new device if possible\r
     for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,\r
                                     &voff, &vlen, &vtype)) != 0; ) {\r
         const char * key = s + koff;\r
@@ -1974,7 +1960,6 @@ MatrixClientRequestDeviceKeys(
                     foundDevice = true;\r
 \r
             if (! foundDevice) {\r
-                printf("new device: %s %s %s\n", d.deviceId, d.deviceKey, d.signingKey);\r
                 client->devices[client->numDevices] = d;\r
                 client->numDevices++;\r
             }\r
index 81b61702d5a4178288c33f011f79936f8917e75e..226a341152b9e298b2984d0086398d3a6a08540c 100644 (file)
@@ -149,6 +149,7 @@ MatrixOlmSessionUnpickle(
     void * pickled, int pickledLen,\r
     const void * key, int keyLen);\r
 \r
+// create an olm sesseion from a type 0 message\r
 bool\r
 MatrixOlmSessionFrom(\r
     MatrixOlmSession * session,\r
@@ -157,6 +158,7 @@ MatrixOlmSessionFrom(
     const char * deviceKey,\r
     const char * encrypted);\r
 \r
+// create a new olm session from a claimed onetime key\r
 bool\r
 MatrixOlmSessionTo(\r
     MatrixOlmSession * session,\r
@@ -239,9 +241,6 @@ typedef struct MatrixClient {
     \r
     MatrixDevice devices[NUM_DEVICES];\r
     int numDevices;\r
-    \r
-    // char deviceKey[DEVICE_KEY_SIZE];\r
-    // char signingKey[DEVICE_KEY_SIZE];\r
 \r
     char userId[USER_ID_SIZE];\r
     char accessToken[ACCESS_TOKEN_SIZE];\r
@@ -315,17 +314,6 @@ MatrixClientSendEventEncrypted(
     const char * msgType,\r
     const char * msgBody);\r
 \r
-void\r
-HandleEvent(\r
-    MatrixClient * client,\r
-    const char * event, int eventLen);\r
-\r
-void\r
-HandleRoomEvent(\r
-    MatrixClient * client,\r
-    const char * room, int roomLen,\r
-    const char * event, int eventLen);\r
-\r
 bool\r
 MatrixClientSync(\r
     MatrixClient * client,\r
@@ -346,25 +334,21 @@ MatrixClientShareMegolmOutSession(
     const char * deviceId,\r
     MatrixMegolmOutSession * session);\r
 \r
-bool\r
-MatrixClientShareMegolmOutSessionTest(\r
-    MatrixClient * client,\r
-    const char * userId,\r
-    const char * deviceId,\r
-    MatrixMegolmOutSession * session);\r
-\r
+// try to lookup outgoing megolm session, return true if found\r
 bool\r
 MatrixClientGetMegolmOutSession(\r
     MatrixClient * client,\r
     const char * roomId,\r
     MatrixMegolmOutSession ** outSession);\r
 \r
+// create a new outgoing megolm session and store it locally\r
 bool\r
 MatrixClientNewMegolmOutSession(\r
     MatrixClient * client,\r
     const char * roomId,\r
     MatrixMegolmOutSession ** outSession);\r
 \r
+// try to lookup incoming megolm session, return true if found\r
 bool\r
 MatrixClientGetMegolmInSession(\r
     MatrixClient * client,\r
@@ -372,6 +356,7 @@ MatrixClientGetMegolmInSession(
     const char * sessionId, int sessionIdLen,\r
     MatrixMegolmInSession ** outSession);\r
 \r
+// create a new incoming megolm session and store it locally\r
 bool\r
 MatrixClientNewMegolmInSession(\r
     MatrixClient * client,\r
@@ -379,7 +364,8 @@ MatrixClientNewMegolmInSession(
     const char * sessionId,\r
     const char * sessionKey,\r
     MatrixMegolmInSession ** outSession);\r
-    \r
+\r
+// send a m.room_key_request to the device identified by userId/devideId\r
 bool\r
 MatrixClientRequestMegolmInSession(\r
     MatrixClient * client,\r
@@ -387,8 +373,9 @@ MatrixClientRequestMegolmInSession(
     const char * sessionId,\r
     const char * senderKey,\r
     const char * userId,\r
-    const char * deviceId); // TODO: remove deviceId (query all devices)\r
+    const char * deviceId);\r
 \r
+// try to lookup olm session, return true if found\r
 bool\r
 MatrixClientGetOlmSession(\r
     MatrixClient * client,\r
@@ -396,6 +383,7 @@ MatrixClientGetOlmSession(
     const char * deviceId,\r
     MatrixOlmSession ** outSession);\r
 \r
+// create a new olm session from a type 0 message and store it locally\r
 bool\r
 MatrixClientNewOlmSessionIn(\r
     MatrixClient * client,\r
@@ -403,7 +391,9 @@ MatrixClientNewOlmSessionIn(
     const char * deviceId,\r
     const char * encrypted,\r
     MatrixOlmSession ** outSession);\r
-    \r
+\r
+// create a new olm session with device userId/deviceId and store it locally\r
+// this automatically claims the onetime key\r
 bool\r
 MatrixClientNewOlmSessionOut(\r
     MatrixClient * client,\r
@@ -433,27 +423,33 @@ MatrixClientSendDummy(
     const char * userId,\r
     const char * deviceId);\r
 \r
+// lookup device key locally and if not present get it from server\r
 bool\r
 MatrixClientRequestDeviceKey(\r
     MatrixClient * client,\r
     const char * deviceId,\r
     char * outDeviceKey, int outDeviceKeyCap);\r
     \r
+// lookup signing key locally and if not present get it from server\r
 bool\r
 MatrixClientRequestSigningKey(\r
     MatrixClient * client,\r
     const char * deviceId,\r
     char * outSigningKey, int outSigningKeyCap);\r
 \r
+// lookup the master key for this user and if not present get it from server\r
 bool\r
 MatrixClientRequestMasterKey(\r
     MatrixClient * client,\r
     char * outMasterKey, int outMasterKeyCap);\r
 \r
+// call keys/query and store retrieved information\r
+// this is called by the other Request* functions\r
 bool\r
 MatrixClientRequestDeviceKeys(\r
     MatrixClient * client);\r
 \r
+// delete this device on the server\r
 bool\r
 MatrixClientDeleteDevice(\r
     MatrixClient * client);\r
index 2cc4cfe418dce33d03d3a9471848011e150dd594..5406fc87eacc5871f255fc80af8bd08eebe5a657 100644 (file)
@@ -202,8 +202,6 @@ MatrixHttpGet(
     else\r
         authorizationHeader[0] = '\0';\r
 \r
-    printf("GET %s%s\n", hc->host, url);\r
-\r
     hc->data = outResponseBuffer;\r
     hc->dataCap = outResponseCap;\r
     hc->dataLen = 0;\r
@@ -243,8 +241,6 @@ MatrixHttpPost(
     else\r
         authorizationHeader[0] = '\0';\r
 \r
-    printf("POST %s%s\n%s\n", hc->host, url, requestBuffer);\r
-\r
     hc->data = outResponseBuffer;\r
     hc->dataCap = outResponseCap;\r
     hc->dataLen = 0;\r
@@ -286,8 +282,6 @@ MatrixHttpPut(
     else\r
         authorizationHeader[0] = '\0';\r
 \r
-    printf("PUT %s%s\n%s\n", hc->host, url, requestBuffer);\r
-\r
     hc->data = outResponseBuffer;\r
     hc->dataCap = outResponseCap;\r
     hc->dataLen = 0;\r
index 3b6970cb0abee4d64a9e5d6518a2acc49c3fa7a9..2f5d768febc131c94712abed94d934d9f138f14f 100644 (file)
@@ -6,7 +6,6 @@
 #include <stdbool.h>\r
 #include <mongoose.h>\r
 \r
-//#define HTTP_DATA_SIZE 1024\r
 #define AUTHORIZATION_HEADER_LEN 64\r
 \r
 typedef struct MatrixHttpConnection {\r
@@ -55,15 +54,12 @@ MatrixHttpCallback(
     {\r
         // Response\r
         struct mg_http_message *hm = (struct mg_http_message *)ev_data;\r
-        // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);\r
-        // client->dataLen = hm->message.len;\r
+        \r
         memcpy(conn->data, hm->body.ptr, hm->body.len);\r
-        // memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);\r
+        \r
         conn->data[hm->body.len] = '\0';\r
         conn->dataLen = hm->body.len;\r
         conn->dataReceived = true;\r
-\r
-        //printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);\r
     }\r
     if (ev == MG_EV_CLOSE)\r
     {\r
@@ -139,22 +135,11 @@ MatrixHttpGet(
 \r
     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
     if (authenticated)\r
-        sprintf(authorizationHeader,\r
+        snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
             "Authorization: Bearer %s\r\n", hc->accessToken);\r
-        // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
-        //     "Authorization: Bearer %s\r\n", client->accessToken);\r
     else\r
         authorizationHeader[0] = '\0';\r
 \r
-    printf(\r
-        "GET %s HTTP/1.1\r\n"\r
-        "Host: %.*s\r\n"\r
-        "%s"\r
-        "\r\n",\r
-        url,\r
-        host.len, host.ptr,\r
-        authorizationHeader);\r
-\r
     mg_printf(hc->connection,\r
         "GET %s HTTP/1.1\r\n"\r
         "Host: %.*s\r\n"\r
@@ -190,26 +175,11 @@ MatrixHttpPost(
 \r
     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
     if (authenticated)\r
-        sprintf(authorizationHeader,\r
+        snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
             "Authorization: Bearer %s\r\n", hc->accessToken);\r
     else\r
         authorizationHeader[0] = '\0';\r
 \r
-    printf(\r
-            "POST %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(hc->connection,\r
             "POST %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r
@@ -220,9 +190,9 @@ MatrixHttpPost(
             "%s"\r
             "\r\n",\r
             url,\r
-            host.len, host.ptr,\r
+            (int)host.len, host.ptr,\r
             authorizationHeader,\r
-            strlen(requestBuffer),\r
+            (int)strlen(requestBuffer),\r
             requestBuffer);\r
 \r
     hc->data = outResponseBuffer;\r
@@ -251,27 +221,11 @@ MatrixHttpPut(
 \r
     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
     if (authenticated)\r
-        sprintf(authorizationHeader,\r
+        snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
             "Authorization: Bearer %s\r\n", hc->accessToken);\r
     else\r
         authorizationHeader[0] = '\0';\r
 \r
-    \r
-    printf(\r
-            "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(hc->connection,\r
             "PUT %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r
@@ -282,9 +236,9 @@ MatrixHttpPut(
             "%s"\r
             "\r\n",\r
             url,\r
-            host.len, host.ptr,\r
+            (int)host.len, host.ptr,\r
             authorizationHeader,\r
-            strlen(requestBuffer),\r
+            (int)strlen(requestBuffer),\r
             requestBuffer);\r
 \r
     hc->data = outResponseBuffer;\r