]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix.c
generate identity keys
[matrix_esp_thesis] / src / matrix.c
index 28e7634e9b367d4f9bd13a7934195167f6a5d910..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
@@ -294,7 +337,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