]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
generate key upload json + json signing
authorPatrick <patrick.schoenberger@posteo.de>
Thu, 29 Jun 2023 05:13:02 +0000 (07:13 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Thu, 29 Jun 2023 05:13:02 +0000 (07:13 +0200)
examples/Keys.c
examples/SendEncrypted.c
src/matrix.c
src/matrix.h

index 77196ba0dc2c24ee6100a1614bff9331d65754ba..6dc2cc0f2d150749fc087bf3b7c3e084fb789949 100644 (file)
@@ -15,6 +15,10 @@ main(void)
         SERVER);\r
     \r
     MatrixHttpInit(&client);\r
+    \r
+    MatrixClientSetAccessToken(&client, ACCESS_TOKEN);\r
+    MatrixClientSetDeviceId(&client, DEVICE_ID);\r
+    MatrixClientSetUserId(&client, USER_ID);\r
 \r
     MatrixClientGenerateOnetimeKeys(&client,\r
         10);\r
index 0c4a7c818022479766154f43f2b699453e70a201..f2bb8dffa38ee163daec4cfbe17805392be598a2 100644 (file)
@@ -4,7 +4,8 @@
 #define SERVER       "https://matrix.org"\r
 #define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"\r
 #define DEVICE_ID    "MAZNCCZLBR"\r
-#define ROOM_ID      "!koVStwyiiKcBVbXZYz:matrix.org"\r
+#define USER_ID      "@pscho:matrix.org"\r
+#define ROOM_ID      "!XKFUjAsGrSSrpDFIxB:matrix.org"\r
 \r
 int\r
 main(void)\r
@@ -19,6 +20,8 @@ main(void)
         ACCESS_TOKEN);\r
     MatrixClientSetDeviceId(&client,\r
         DEVICE_ID);\r
+    MatrixClientSetUserId(&client,\r
+        USER_ID);\r
 \r
     // MatrixMegolmOutSession megolmOutSession;\r
     // MatrixMegolmOutSessionInit(&megolmOutSession);\r
@@ -31,6 +34,10 @@ main(void)
         ROOM_ID,\r
         "m.room.message",\r
         "{\"body\":\"Hello\",\"msgtype\":\"m.text\"}");\r
+\r
+    MatrixClientShareMegolmOutSessionTest(&client,\r
+        "ULZZOKJBYN",\r
+        &client.megolmOutSessions[0]);\r
         \r
     MatrixHttpDeinit(&client);\r
 \r
index 33988b4a12710c35419ce363513b4c4cad21fc94..d1878c5057f0d5bcc93f2b69e54190c262b99f0c 100644 (file)
 #define KEYS_QUERY_REQUEST_SIZE 256\r
 #define KEYS_QUERY_RESPONSE_SIZE 1024\r
 \r
+#define UPLOAD_KEYS_REQUEST_SIZE 512\r
+#define UPLOAD_KEYS_REQUEST_SIGNED_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
@@ -65,6 +70,61 @@ JsonEscape(
     return true;\r
 }\r
 \r
+bool JsonSign(\r
+    MatrixClient * client,\r
+    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
@@ -158,22 +218,13 @@ MatrixClientInit(
     strcpy(client->server, server);\r
 \r
     // init olm account\r
-    client->olmAccount = olm_account(client->olmAccountMemory);\r
-\r
-    static uint8_t random[OLM_ACCOUNT_RANDOM_SIZE];\r
-    Randomize(random, OLM_ACCOUNT_RANDOM_SIZE);\r
-\r
-    size_t res;\r
-    res = olm_create_account(\r
-        client->olmAccount,\r
-        random,\r
-        OLM_ACCOUNT_RANDOM_SIZE);\r
+    MatrixOlmAccountInit(&client->olmAccount);\r
 \r
     // set device key\r
     static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];\r
-    res =\r
+    size_t res =\r
         olm_account_identity_keys(\r
-            client->olmAccount,\r
+            client->olmAccount.account,\r
             deviceKeysJson,\r
             OLM_IDENTITY_KEYS_JSON_SIZE);\r
 \r
@@ -219,6 +270,113 @@ MatrixClientSetDeviceId(
     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
+bool\r
+MatrixClientUploadOnetimeKeys(\r
+    MatrixClient * client)\r
+{\r
+    static char requestBuffer[UPLOAD_KEYS_REQUEST_SIZE];\r
+\r
+    mjson_snprintf(requestBuffer, UPLOAD_KEYS_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), UPLOAD_KEYS_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
+        "}}");\r
+\r
+    printf("%s\n", requestBuffer);\r
+\r
+    return true;\r
+}\r
+\r
+bool\r
+MatrixClientUploadDeviceKeys(\r
+    MatrixClient * client)\r
+{\r
+    static char deviceKeysBuffer[UPLOAD_KEYS_REQUEST_SIZE];\r
+\r
+    mjson_snprintf(deviceKeysBuffer, UPLOAD_KEYS_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[UPLOAD_KEYS_REQUEST_SIGNED_SIZE];\r
+    JsonSign(client,\r
+        deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
+        deviceKeysSignedBuffer, UPLOAD_KEYS_REQUEST_SIZE);\r
+\r
+    printf("%s\n", deviceKeysSignedBuffer);\r
+\r
+    return true;\r
+}\r
+\r
 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login\r
 bool\r
 MatrixClientLoginPassword(\r
@@ -405,6 +563,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
index 60561aa04087d4c0c81d5881a9a235e216120e18..38fb767742cdaf23948313d06ac47a6cfe09d9e3 100644 (file)
 #define KEY_SHARE_EVENT_LEN 1024\r
 \r
 #define OLM_ACCOUNT_MEMORY_SIZE 7528\r
-#define OLM_ACCOUNT_RANDOM_SIZE 32+32\r
+#define OLM_ACCOUNT_RANDOM_SIZE (32+32)\r
 \r
 #define OLM_SESSION_MEMORY_SIZE 3352\r
 #define OLM_ENCRYPT_RANDOM_SIZE 32\r
 \r
+#define OLM_ONETIME_KEYS_RANDOM_SIZE 32*10\r
+#define OLM_KEY_ID_SIZE 32\r
+\r
+#define OLM_SIGNATURE_SIZE 128\r
+\r
 #define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232\r
 #define MEGOLM_SESSION_ID_SIZE 44\r
 #define MEGOLM_SESSION_KEY_SIZE 306\r
 #define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32)\r
 \r
+#define JSON_ONETIME_KEY_SIZE 128\r
+#define JSON_ONETIME_KEY_SIGNED_SIZE 256\r
+#define JSON_SIGNATURE_SIZE 256\r
+\r
 #define NUM_MEGOLM_SESSIONS 10\r
 #define NUM_OLM_SESSIONS 10\r
 #define NUM_DEVICES 10\r
@@ -46,12 +55,33 @@ bool
 JsonEscape(\r
     char * sIn, int sInLen,\r
     char * sOut, int sOutCap);\r
+    \r
+bool JsonSign(\r
+    char * sIn, int sInLen,\r
+    char * sOut, int sOutCap);\r
+\r
+// Matrix Device\r
 \r
 typedef struct MatrixDevice {\r
     char deviceId[DEVICE_ID_SIZE];\r
     char deviceKey[DEVICE_KEY_SIZE];\r
 } MatrixDevice;\r
 \r
+\r
+// Matrix Olm Account\r
+\r
+typedef struct MatrixOlmAccount {\r
+    OlmAccount * account;\r
+    char memory[OLM_ACCOUNT_MEMORY_SIZE];\r
+} MatrixOlmAccount;\r
+\r
+bool\r
+MatrixOlmAccountInit(\r
+    MatrixOlmAccount * account);\r
+\r
+\r
+// Matrix Olm Session\r
+\r
 typedef struct MatrixOlmSession {\r
     const char * deviceId;\r
 \r
@@ -72,6 +102,7 @@ MatrixOlmSessionEncrypt(
     char * outBuffer, int outBufferCap);\r
 \r
 \r
+// Matrix Megolm Session\r
 \r
 typedef struct MatrixMegolmInSession {\r
     OlmInboundGroupSession * session;\r
@@ -99,10 +130,10 @@ MatrixMegolmOutSessionEncrypt(
     char * outBuffer, int outBufferCap);\r
 \r
 \r
+// Matrix Client\r
 \r
 typedef struct MatrixClient {\r
-    OlmAccount * olmAccount;\r
-    char olmAccountMemory[OLM_ACCOUNT_MEMORY_SIZE];\r
+    MatrixOlmAccount olmAccount;\r
 \r
     MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
     int numMegolmInSessions;\r
@@ -142,6 +173,24 @@ MatrixClientSetDeviceId(
     MatrixClient * client,\r
     const char * deviceId);\r
 \r
+bool\r
+MatrixClientSetUserId(\r
+    MatrixClient * client,\r
+    const char * userId);\r
+\r
+bool\r
+MatrixClientGenerateOnetimeKeys(\r
+    MatrixClient * client,\r
+    int numberOfKeys);\r
+\r
+bool\r
+MatrixClientUploadOnetimeKeys(\r
+    MatrixClient * client);\r
+\r
+bool\r
+MatrixClientUploadDeviceKeys(\r
+    MatrixClient * client);\r
+\r
 bool\r
 MatrixClientLoginPassword(\r
     MatrixClient * client,\r
@@ -174,6 +223,12 @@ MatrixClientShareMegolmOutSession(
     const char * deviceId,\r
     MatrixMegolmOutSession * session);\r
 \r
+bool\r
+MatrixClientShareMegolmOutSessionTest(\r
+    MatrixClient * client,\r
+    const char * deviceId,\r
+    MatrixMegolmOutSession * session);\r
+\r
 bool\r
 MatrixClientGetMegolmOutSession(\r
     MatrixClient * client,\r