]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
generate identity keys
authorPatrick <patrick.schoenberger@posteo.de>
Thu, 22 Jun 2023 13:56:21 +0000 (15:56 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Thu, 22 Jun 2023 13:56:21 +0000 (15:56 +0200)
examples/SendEncrypted.c
src/matrix.c
src/matrix.h

index db2f83cb9e1710099a29ca255b92fbfc5e2dc968..0c4a7c818022479766154f43f2b699453e70a201 100644 (file)
@@ -17,6 +17,8 @@ main(void)
 \r
     MatrixClientSetAccessToken(&client,\r
         ACCESS_TOKEN);\r
+    MatrixClientSetDeviceId(&client,\r
+        DEVICE_ID);\r
 \r
     // MatrixMegolmOutSession megolmOutSession;\r
     // MatrixMegolmOutSessionInit(&megolmOutSession);\r
index cc34a0a727bcc74915552f4efb02e12b5f265ab5..33988b4a12710c35419ce363513b4c4cad21fc94 100644 (file)
@@ -157,6 +157,33 @@ MatrixClientInit(
 \r
     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
+\r
+    // set device key\r
+    static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];\r
+    res =\r
+        olm_account_identity_keys(\r
+            client->olmAccount,\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 +194,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 +203,22 @@ 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
 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login\r
 bool\r
 MatrixClientLoginPassword(\r
index 6538e12d7fa6f16a464d388e11fa41edc18dc44f..60561aa04087d4c0c81d5881a9a235e216120e18 100644 (file)
 #define REFRESH_TOKEN_SIZE 20\r
 #define MAX_URL_LEN 128\r
 \r
-#define DEVICE_KEY_SIZE 20\r
+#define OLM_IDENTITY_KEYS_JSON_SIZE 128\r
+#define DEVICE_KEY_SIZE 44\r
+#define SIGNING_KEY_SIZE 44\r
 \r
 #define KEY_SHARE_EVENT_LEN 1024\r
 \r
+#define OLM_ACCOUNT_MEMORY_SIZE 7528\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
@@ -97,7 +102,7 @@ MatrixMegolmOutSessionEncrypt(
 \r
 typedef struct MatrixClient {\r
     OlmAccount * olmAccount;\r
-    OlmSession * olmSession;\r
+    char olmAccountMemory[OLM_ACCOUNT_MEMORY_SIZE];\r
 \r
     MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
     int numMegolmInSessions;\r
@@ -110,6 +115,7 @@ typedef struct MatrixClient {
     int numDevices;\r
     \r
     char deviceKey[DEVICE_KEY_SIZE];\r
+    char signingKey[DEVICE_KEY_SIZE];\r
 \r
     char userId[USER_ID_SIZE];\r
     char server[SERVER_SIZE];\r
@@ -131,6 +137,11 @@ MatrixClientSetAccessToken(
     MatrixClient * client,\r
     const char * accessToken);\r
 \r
+bool\r
+MatrixClientSetDeviceId(\r
+    MatrixClient * client,\r
+    const char * deviceId);\r
+\r
 bool\r
 MatrixClientLoginPassword(\r
     MatrixClient * client,\r