]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix.c
work on SendEncrypted
[matrix_esp_thesis] / src / matrix.c
index 28e7634e9b367d4f9bd13a7934195167f6a5d910..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 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
 \r
 \r
 void\r
-Randomize(uint8_t * random, int randomLen)\r
+Randomize(\r
+    uint8_t * random,\r
+    int randomLen)\r
 {\r
     static bool first = false;\r
     if (first) { srand(time(0)); first = false; }\r
@@ -40,7 +51,7 @@ Randomize(uint8_t * random, int randomLen)
 \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
@@ -65,22 +76,86 @@ JsonEscape(
     return true;\r
 }\r
 \r
+bool JsonSign(\r
+    MatrixClient * client,\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap)\r
+{\r
+    static char signature[OLM_SIGNATURE_SIZE];\r
+    size_t res =\r
+        olm_account_sign(client->olmAccount.account,\r
+            sIn, sInLen,\r
+            signature, OLM_SIGNATURE_SIZE);\r
+    \r
+    int signatureLen = res;\r
+\r
+    static char signatureJson[JSON_SIGNATURE_SIZE];\r
+    int signatureJsonLen =\r
+        mjson_snprintf(signatureJson, JSON_SIGNATURE_SIZE,\r
+            "{"\r
+                "\"signatures\":{"\r
+                    "\"%s\":{"\r
+                        "\"ed25519:%s\":\"%.*s\""\r
+                    "}"\r
+                "}"\r
+            "}",\r
+            client->userId,\r
+            client->deviceId,\r
+            signatureLen, signature);\r
+\r
+    struct mjson_fixedbuf result = { sOut, sOutCap, 0 };\r
+    mjson_merge(\r
+        sIn, sInLen,\r
+        signatureJson, signatureJsonLen,\r
+        mjson_print_fixed_buf,\r
+        &result);\r
+\r
+    return true;\r
+}\r
+\r
+\r
+bool\r
+MatrixOlmAccountInit(\r
+    MatrixOlmAccount * account)\r
+{\r
+    account->account = olm_account(account->memory);\r
+\r
+    static uint8_t random[OLM_ACCOUNT_RANDOM_SIZE];\r
+    Randomize(random, OLM_ACCOUNT_RANDOM_SIZE);\r
+\r
+    size_t res = olm_create_account(\r
+        account->account,\r
+        random,\r
+        OLM_ACCOUNT_RANDOM_SIZE);\r
+\r
+    return res != olm_error();\r
+}\r
+\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
@@ -157,6 +232,24 @@ MatrixClientInit(
 \r
     strcpy(client->server, server);\r
 \r
+    // init olm account\r
+    MatrixOlmAccountInit(&client->olmAccount);\r
+\r
+    // set device key\r
+    static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];\r
+    size_t res =\r
+        olm_account_identity_keys(\r
+            client->olmAccount.account,\r
+            deviceKeysJson,\r
+            OLM_IDENTITY_KEYS_JSON_SIZE);\r
+\r
+    mjson_get_string(deviceKeysJson, res,\r
+        "$.curve25519",\r
+        client->deviceKey, DEVICE_KEY_SIZE);\r
+    mjson_get_string(deviceKeysJson, res,\r
+        "$.ed25519",\r
+        client->signingKey, SIGNING_KEY_SIZE);\r
+\r
     return true;\r
 }\r
 \r
@@ -167,7 +260,7 @@ MatrixClientSetAccessToken(
 {\r
     int accessTokenLen = strlen(accessToken);\r
 \r
-    if (accessTokenLen < ACCESS_TOKEN_SIZE - 1)\r
+    if (accessTokenLen > ACCESS_TOKEN_SIZE - 1)\r
         return false;\r
 \r
     for (int i = 0; i < accessTokenLen; i++)\r
@@ -176,6 +269,200 @@ MatrixClientSetAccessToken(
     return true;\r
 }\r
 \r
+bool\r
+MatrixClientSetDeviceId(\r
+    MatrixClient * client,\r
+    const char * deviceId)\r
+{\r
+    int deviceIdLen = strlen(deviceId);\r
+\r
+    if (deviceIdLen > DEVICE_ID_SIZE - 1)\r
+        return false;\r
+\r
+    for (int i = 0; i < deviceIdLen; i++)\r
+        client->deviceId[i] = deviceId[i];\r
+\r
+    return true;\r
+}\r
+\r
+bool\r
+MatrixClientSetUserId(\r
+    MatrixClient * client,\r
+    const char * userId)\r
+{\r
+    int userIdLen = strlen(userId);\r
+\r
+    if (userIdLen > USER_ID_SIZE - 1)\r
+        return false;\r
+\r
+    for (int i = 0; i < userIdLen; i++)\r
+        client->userId[i] = userId[i];\r
+\r
+    return true;\r
+}\r
+\r
+bool\r
+MatrixClientGenerateOnetimeKeys(\r
+    MatrixClient * client,\r
+    int numberOfKeys)\r
+{\r
+    static uint8_t random[OLM_ONETIME_KEYS_RANDOM_SIZE];\r
+    Randomize(random, OLM_ONETIME_KEYS_RANDOM_SIZE);\r
+\r
+    size_t res =\r
+        olm_account_generate_one_time_keys(client->olmAccount.account,\r
+            numberOfKeys, random, OLM_ONETIME_KEYS_RANDOM_SIZE);\r
+\r
+    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[KEYS_UPLOAD_REQUEST_SIZE];\r
+\r
+    mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+        "{\"one_time_keys\":{");\r
+\r
+    static char onetimeKeysBuffer[1024];\r
+    olm_account_one_time_keys(client->olmAccount.account,\r
+        onetimeKeysBuffer, 1024);\r
+\r
+    const char *keys;\r
+    int keysLen;\r
+    mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen);\r
+\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
+        \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
+            keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);\r
+        \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)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
+        "}}");\r
+\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[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, client->deviceKey,\r
+        client->deviceId, client->signingKey,\r
+        client->userId);\r
+\r
+    static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+    JsonSign(client,\r
+        deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+        deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
+\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
 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login\r
 bool\r
 MatrixClientLoginPassword(\r
@@ -294,7 +581,7 @@ MatrixClientSendEventEncrypted(
         "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
         "\"sender_key\":\"%s\","\r
         "\"ciphertext\":\"%s\","\r
-        "\"session_id\":%s,"\r
+        "\"session_id\":\"%s\","\r
         "\"device_id\":\"%s\""\r
         "}",\r
         senderKey,\r
@@ -325,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
@@ -344,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
@@ -362,6 +650,36 @@ MatrixClientShareMegolmOutSession(
     return true;\r
 }\r
 \r
+bool\r
+MatrixClientShareMegolmOutSessionTest(\r
+    MatrixClient * client,\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
+        client->userId,\r
+        deviceId,\r
+        eventBuffer,\r
+        "m.room_key");\r
+\r
+    return true;\r
+}\r
+\r
 // bool\r
 // MatrixClientSetMegolmOutSession(\r
 //     MatrixClient * client,\r
@@ -413,6 +731,7 @@ MatrixClientGetMegolmOutSession(
 bool\r
 MatrixClientGetOlmSession(\r
     MatrixClient * client,\r
+    const char * userId,\r
     const char * deviceId,\r
     MatrixOlmSession ** outSession)\r
 {\r
@@ -427,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
@@ -488,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
@@ -592,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