]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix.c
work on SendEncrypted
[matrix_esp_thesis] / src / matrix.c
index d1878c5057f0d5bcc93f2b69e54190c262b99f0c..966a0389c23cc8b39228a5d00aee2c5eab08cf55 100644 (file)
 #define ROOMEVENT_RESPONSE_SIZE 1024\r
 #define ROOMEVENT_URL "/_matrix/client/v3/rooms/%s/send/%s/%d"\r
 \r
-#define TODEVICE_EVENT_SIZE 512\r
+#define TODEVICE_EVENT_SIZE (1024*5)\r
 #define TODEVICE_URL "/_matrix/client/v3/sendToDevice/%s/%d"\r
 \r
 #define KEYS_QUERY_URL "/_matrix/client/v3/keys/query"\r
 #define KEYS_QUERY_REQUEST_SIZE 256\r
-#define KEYS_QUERY_RESPONSE_SIZE 1024\r
+#define KEYS_QUERY_RESPONSE_SIZE (1024*10)\r
 \r
-#define UPLOAD_KEYS_REQUEST_SIZE 512\r
-#define UPLOAD_KEYS_REQUEST_SIGNED_SIZE 1024\r
+#define KEYS_UPLOAD_URL "/_matrix/client/v3/keys/upload"\r
+#define KEYS_UPLOAD_REQUEST_SIZE 1024\r
+#define KEYS_UPLOAD_REQUEST_SIGNED_SIZE 2048\r
+#define KEYS_UPLOAD_RESPONSE_SIZE 2048\r
+\r
+#define KEYS_CLAIM_URL "/_matrix/client/v3/keys/claim"\r
+#define KEYS_CLAIM_REQUEST_SIZE 1024\r
+#define KEYS_CLAIM_RESPONSE_SIZE 1024\r
 \r
 #define JSON_QUERY_SIZE 128\r
 \r
@@ -45,7 +51,7 @@ Randomize(
 \r
 bool\r
 JsonEscape(\r
-    char * sIn, int sInLen,\r
+    const char * sIn, int sInLen,\r
     char * sOut, int sOutCap)\r
 {\r
     int sOutIndex = 0;\r
@@ -72,7 +78,7 @@ JsonEscape(
 \r
 bool JsonSign(\r
     MatrixClient * client,\r
-    char * sIn, int sInLen,\r
+    const char * sIn, int sInLen,\r
     char * sOut, int sOutCap)\r
 {\r
     static char signature[OLM_SIGNATURE_SIZE];\r
@@ -127,20 +133,29 @@ MatrixOlmAccountInit(
 \r
 // TODO: in/outbound sessions\r
 bool\r
-MatrixOlmSessionInit(\r
+MatrixOlmSessionFrom(\r
     MatrixOlmSession * session,\r
-    const char * deviceId)\r
+    OlmAccount * olmAccount,\r
+    const char * deviceId,\r
+    const char * deviceKey,\r
+    const char * deviceOnetimeKey)\r
 {\r
     memset(session, 0, sizeof(MatrixOlmSession));\r
 \r
-    static uint8_t random[MEGOLM_INIT_RANDOM_SIZE];\r
-    Randomize(random, MEGOLM_INIT_RANDOM_SIZE);\r
-\r
     session->deviceId = deviceId;\r
 \r
     session->session =\r
         olm_session(session->memory);\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
+\r
     return session->session != NULL;\r
 }\r
 \r
@@ -301,13 +316,14 @@ MatrixClientGenerateOnetimeKeys(
     return res != olm_error();\r
 }\r
 \r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
 bool\r
 MatrixClientUploadOnetimeKeys(\r
     MatrixClient * client)\r
 {\r
-    static char requestBuffer[UPLOAD_KEYS_REQUEST_SIZE];\r
+    static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
 \r
-    mjson_snprintf(requestBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
+    mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
         "{\"one_time_keys\":{");\r
 \r
     static char onetimeKeysBuffer[1024];\r
@@ -332,27 +348,33 @@ MatrixClientUploadOnetimeKeys(
             keyJson, JSON_ONETIME_KEY_SIZE,\r
             keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);\r
         \r
-        mjson_snprintf(requestBuffer+strlen(requestBuffer), UPLOAD_KEYS_REQUEST_SIZE-strlen(requestBuffer),\r
+        mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
             "\"signed_curve25519:%.*s\":%s,",\r
             klen-2, keys + koff+1,\r
             keyJsonSigned);\r
     }\r
 \r
-    mjson_snprintf(requestBuffer+strlen(requestBuffer), UPLOAD_KEYS_REQUEST_SIZE-strlen(requestBuffer),\r
+    mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
         "}}");\r
 \r
-    printf("%s\n", requestBuffer);\r
+    static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
+    MatrixHttpPost(client,\r
+        KEYS_UPLOAD_URL,\r
+        requestBuffer,\r
+        responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
+        true);\r
 \r
     return true;\r
 }\r
 \r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
 bool\r
 MatrixClientUploadDeviceKeys(\r
     MatrixClient * client)\r
 {\r
-    static char deviceKeysBuffer[UPLOAD_KEYS_REQUEST_SIZE];\r
+    static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
 \r
-    mjson_snprintf(deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE,\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
@@ -367,13 +389,77 @@ MatrixClientUploadDeviceKeys(
         client->deviceId, client->signingKey,\r
         client->userId);\r
 \r
-    static char deviceKeysSignedBuffer[UPLOAD_KEYS_REQUEST_SIGNED_SIZE];\r
+    static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
     JsonSign(client,\r
-        deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
-        deviceKeysSignedBuffer, UPLOAD_KEYS_REQUEST_SIZE);\r
+        deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+        deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
 \r
-    printf("%s\n", deviceKeysSignedBuffer);\r
 \r
+    static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
+    MatrixHttpPost(client,\r
+        KEYS_UPLOAD_URL,\r
+        deviceKeysSignedBuffer,\r
+        responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
+        true);\r
+\r
+    return true;\r
+}\r
+\r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysclaim\r
+bool\r
+MatrixClientClaimOnetimeKey(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    char * outOnetimeKey, int outOnetimeKeyCap)\r
+{\r
+    static char requestBuffer[KEYS_CLAIM_REQUEST_SIZE];\r
+    mjson_snprintf(requestBuffer, KEYS_CLAIM_REQUEST_SIZE,\r
+    "{"\r
+      "\"one_time_keys\": {"\r
+        "\"%s\": {"\r
+          "\"%s\": \"signed_curve25519\""\r
+        "}"\r
+      "},"\r
+      "\"timeout\": 10000"\r
+    "}",\r
+    userId,\r
+    deviceId);\r
+\r
+    static char responseBuffer[KEYS_CLAIM_RESPONSE_SIZE];\r
+    MatrixHttpPost(client,\r
+        KEYS_CLAIM_URL,\r
+        requestBuffer,\r
+        responseBuffer, KEYS_CLAIM_RESPONSE_SIZE,\r
+        true);\r
+    \r
+    char userIdEscaped[USER_ID_SIZE];\r
+    JsonEscape(userId, strlen(userId),\r
+        userIdEscaped, USER_ID_SIZE);\r
+    \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
+    const char * keyObject;\r
+    int keyObjectSize;\r
+    mjson_find(responseBuffer, strlen(responseBuffer),\r
+        query,\r
+        &keyObject, &keyObjectSize);\r
+    \r
+    int koff, klen, voff, vlen, vtype;\r
+    mjson_next(keyObject, keyObjectSize, 0,\r
+        &koff, &klen, &voff, &vlen, &vtype);\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
     return true;\r
 }\r
 \r
@@ -526,11 +612,12 @@ MatrixClientSync(
 bool\r
 MatrixClientShareMegolmOutSession(\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
+    static char eventBuffer[KEY_SHARE_EVENT_LEN];\r
     sprintf(eventBuffer,\r
         "{"\r
             "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
@@ -545,7 +632,7 @@ MatrixClientShareMegolmOutSession(
 \r
     // get olm session\r
     MatrixOlmSession * olmSession;\r
-    MatrixClientGetOlmSession(client, deviceId, &olmSession);\r
+    MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);\r
 \r
     // encrypt\r
     char encryptedBuffer[KEY_SHARE_EVENT_LEN];\r
@@ -644,6 +731,7 @@ MatrixClientGetMegolmOutSession(
 bool\r
 MatrixClientGetOlmSession(\r
     MatrixClient * client,\r
+    const char * userId,\r
     const char * deviceId,\r
     MatrixOlmSession ** outSession)\r
 {\r
@@ -658,9 +746,23 @@ MatrixClientGetOlmSession(
 \r
     if (client->numOlmSessions < NUM_OLM_SESSIONS)\r
     {\r
-        MatrixOlmSessionInit(\r
+        static char deviceKey[DEVICE_KEY_SIZE];\r
+        MatrixClientGetDeviceKey(client,\r
+            deviceId,\r
+            deviceKey, DEVICE_KEY_SIZE);\r
+\r
+        char onetimeKey[ONETIME_KEY_SIZE];\r
+        MatrixClientClaimOnetimeKey(client,\r
+            userId,\r
+            deviceId,\r
+            onetimeKey, ONETIME_KEY_SIZE);\r
+\r
+        MatrixOlmSessionFrom(\r
             &client->olmSessions[client->numOlmSessions],\r
-            deviceId);\r
+            client->olmAccount.account,\r
+            deviceId,\r
+            deviceKey,\r
+            onetimeKey);\r
 \r
         *outSession = &client->olmSessions[client->numOlmSessions];\r
         \r
@@ -719,7 +821,7 @@ MatrixClientSendToDeviceEncrypted(
 {\r
     // get olm session\r
     MatrixOlmSession * olmSession;\r
-    MatrixClientGetOlmSession(client, deviceId, &olmSession);\r
+    MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);\r
 \r
     // create event json\r
     char deviceKey[DEVICE_KEY_SIZE];\r
@@ -823,68 +925,65 @@ bool
 MatrixClientRequestDeviceKeys(\r
     MatrixClient * client)\r
 {\r
-    char userIdEscaped[USER_ID_SIZE];\r
+    static char userIdEscaped[USER_ID_SIZE];\r
     JsonEscape(client->userId, strlen(client->userId),\r
         userIdEscaped, USER_ID_SIZE);\r
 \r
-    char request[KEYS_QUERY_REQUEST_SIZE];\r
+    static char request[KEYS_QUERY_REQUEST_SIZE];\r
     snprintf(request, KEYS_QUERY_REQUEST_SIZE,\r
-        "{\"device_keys\":{\"%s\":[]}}", userIdEscaped);\r
+        "{\"device_keys\":{\"%s\":[]}}", client->userId);\r
 \r
-    char responseBuffer[KEYS_QUERY_RESPONSE_SIZE];\r
+    static char responseBuffer[KEYS_QUERY_RESPONSE_SIZE];\r
     bool requestResult = MatrixHttpPost(client,\r
         KEYS_QUERY_URL,\r
         request,\r
         responseBuffer, KEYS_QUERY_RESPONSE_SIZE,\r
         true);\r
 \r
-    if (requestResult)\r
-    {\r
-        // query for retrieving device keys for user id\r
-        char query[JSON_QUERY_SIZE];\r
-        snprintf(query, JSON_QUERY_SIZE,\r
-            "$.device_keys.%s", userIdEscaped);\r
-        \r
-        const char * s;\r
-        int slen;\r
-        mjson_find(responseBuffer, strlen(responseBuffer),\r
-            query, &s, &slen);\r
+    if (! requestResult)\r
+        return false;\r
 \r
-        // loop over keys\r
-        \r
-        int koff, klen, voff, vlen, vtype, off;\r
-        for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,\r
-                                        &voff, &vlen, &vtype)) != 0; ) {\r
-            const char * key = s + koff;\r
-            const char * val = s + voff;\r
-\r
-            // set device id, "key" is the JSON key\r
-            MatrixDevice d;\r
-            strncpy(d.deviceId, key, klen);\r
-\r
-            // look for device key in value\r
-            char deviceKeyQuery[JSON_QUERY_SIZE];\r
-            snprintf(deviceKeyQuery, JSON_QUERY_SIZE,\r
-                "$.keys.curve25519:%.*s", klen, key);\r
-            mjson_get_string(val, vlen,\r
-                deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE);\r
-\r
-            // add device\r
-            if (client->numDevices < NUM_DEVICES)\r
-            {\r
-                client->devices[client->numDevices] = d;\r
-                client->numDevices++;\r
-            }\r
-            else\r
-            {\r
-                return false;\r
-            }\r
-        }\r
+    // query for retrieving device keys for user id\r
+    static char query[JSON_QUERY_SIZE];\r
+    snprintf(query, JSON_QUERY_SIZE,\r
+        "$.device_keys.%s", userIdEscaped);\r
+    \r
+    const char * s;\r
+    int slen;\r
+    mjson_find(responseBuffer, strlen(responseBuffer),\r
+        query, &s, &slen);\r
 \r
-        return true;\r
-    }\r
-    else\r
-    {\r
-        return false;\r
+    // loop over keys\r
+    \r
+    int koff, klen, voff, vlen, vtype, off = 0;\r
+    for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,\r
+                                    &voff, &vlen, &vtype)) != 0; ) {\r
+        const char * key = s + koff;\r
+        const char * val = s + voff;\r
+\r
+        // set device id, "key" is the JSON key\r
+        MatrixDevice d;\r
+        snprintf(d.deviceId, DEVICE_ID_SIZE,\r
+            "%.*s", klen-2, key+1);\r
+\r
+        // look for device key in value\r
+        static char deviceKeyQuery[JSON_QUERY_SIZE];\r
+        snprintf(deviceKeyQuery, JSON_QUERY_SIZE,\r
+            "$.keys.curve25519:%s", d.deviceId);\r
+        mjson_get_string(val, vlen,\r
+            deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE);\r
+\r
+        // add device\r
+        if (client->numDevices < NUM_DEVICES)\r
+        {\r
+            client->devices[client->numDevices] = d;\r
+            client->numDevices++;\r
+        }\r
+        else\r
+        {\r
+            return false;\r
+        }\r
     }\r
+\r
+    return true;\r
 }\r