+\r
+// https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3sync\r
+bool\r
+MatrixClientSync(\r
+ MatrixClient * client,\r
+ char * outSyncBuffer, int outSyncCap,\r
+ const char * nextBatch)\r
+{\r
+ // filter={\"event_fields\":[\"to_device\"]}\r
+ static char url[MAX_URL_LEN];\r
+ snprintf(url, MAX_URL_LEN,\r
+ "/_matrix/client/v3/sync?timeout=%d%s",\r
+ SYNC_TIMEOUT,\r
+ strlen(nextBatch) > 0 ? "&since=" : "");\r
+ \r
+ int index = strlen(url);\r
+\r
+ for (size_t i = 0; i < strlen(nextBatch); i++) {\r
+ char c = nextBatch[i];\r
+\r
+ if (c == '~') {\r
+ url[index++] = '%';\r
+ url[index++] = '7';\r
+ url[index++] = 'E';\r
+ }\r
+ else {\r
+ url[index++] = c;\r
+ }\r
+ }\r
+ url[index] = '\0';\r
+\r
+ return\r
+ MatrixHttpGet(client,\r
+ url,\r
+ outSyncBuffer, outSyncCap,\r
+ true);\r
+}\r
+\r
+// https://spec.matrix.org/v1.7/client-server-api/#get_matrixclientv3roomsroomideventeventid\r
+bool\r
+MatrixClientGetRoomEvent(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * eventId,\r
+ char * outEvent, int outEventCap)\r
+{\r
+ static char url[MAX_URL_LEN];\r
+ snprintf(url, MAX_URL_LEN,\r
+ "/_matrix/client/v3/rooms/%s/event/%s",\r
+ roomId,\r
+ eventId);\r
+\r
+ return\r
+ MatrixHttpGet(client,\r
+ url,\r
+ outEvent, outEventCap,\r
+ true);\r
+}\r
+\r
+bool\r
+MatrixClientShareMegolmOutSession(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ MatrixMegolmOutSession * session)\r
+{\r
+ // generate room key event\r
+ static 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
+ MatrixClientSendToDeviceEncrypted(client,\r
+ userId,\r
+ deviceId,\r
+ eventBuffer,\r
+ "m.room_key");\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientShareMegolmOutSessionTest(\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
+ 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
+ userId,\r
+ deviceId,\r
+ eventBuffer,\r
+ "m.room_key");\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientGetMegolmOutSession(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ MatrixMegolmOutSession ** outSession)\r
+{\r
+ for (int i = 0; i < client->numMegolmOutSessions; i++)\r
+ {\r
+ if (strcmp(client->megolmOutSessions[i].roomId, roomId) == 0)\r
+ {\r
+ *outSession = &client->megolmOutSessions[i];\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientNewMegolmOutSession(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ MatrixMegolmOutSession ** outSession)\r
+{\r
+ if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)\r
+ {\r
+ MatrixMegolmOutSession * result =\r
+ &client->megolmOutSessions[client->numMegolmOutSessions];\r
+ \r
+ MatrixMegolmOutSessionInit(result,\r
+ roomId);\r
+\r
+ *outSession = result;\r
+\r
+ client->numMegolmOutSessions++;\r
+\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientGetMegolmInSession(\r
+ MatrixClient * client,\r
+ const char * roomId, int roomIdLen,\r
+ const char * sessionId, int sessionIdLen,\r
+ MatrixMegolmInSession ** outSession)\r
+{\r
+ for (int i = 0; i < client->numMegolmInSessions; i++)\r
+ {\r
+ if (strncmp(client->megolmInSessions[i].roomId, roomId, roomIdLen) == 0 &&\r
+ strncmp(client->megolmInSessions[i].id, sessionId, sessionIdLen) == 0)\r
+ {\r
+ *outSession = &client->megolmInSessions[i];\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientNewMegolmInSession(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * sessionId,\r
+ const char * sessionKey,\r
+ MatrixMegolmInSession ** outSession)\r
+{\r
+ if (client->numMegolmInSessions < NUM_MEGOLM_SESSIONS)\r
+ {\r
+ MatrixMegolmInSession * result =\r
+ &client->megolmInSessions[client->numMegolmInSessions];\r
+ \r
+ MatrixMegolmInSessionInit(result,\r
+ roomId,\r
+ sessionId,\r
+ sessionKey, strlen(sessionKey));\r
+ \r
+ *outSession = result;\r
+\r
+ client->numMegolmInSessions++;\r
+\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientRequestMegolmInSession(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * sessionId,\r
+ const char * senderKey,\r
+ const char * userId,\r
+ const char * deviceId)\r
+{\r
+ // TODO: cancel requests\r
+ MatrixClientSendDummy(client, userId, deviceId);\r
+\r
+ static char event[ROOMKEY_REQUEST_SIZE];\r
+ snprintf(event, ROOMKEY_REQUEST_SIZE,\r
+ "{"\r
+ "\"action\":\"request\","\r
+ "\"body\":{"\r
+ "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
+ "\"room_id\":\"%s\","\r
+ "\"sender_key\":\"%s\","\r
+ "\"session_id\":\"%s\""\r
+ "},"\r
+ "\"request_id\":\"%d\","\r
+ "\"requesting_device_id\":\"%s\""\r
+ "}",\r
+ roomId,\r
+ senderKey,\r
+ sessionId,\r
+ time(NULL),\r
+ client->deviceId);\r
+\r
+ \r
+ MatrixClientSendToDevice(client,\r
+ userId,\r
+ deviceId,\r
+ event,\r
+ "m.room_key_request");\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientGetOlmSessionIn(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ const char * encrypted,\r
+ MatrixOlmSession ** outSession)\r
+{\r
+ for (int i = 0; i < client->numOlmSessions; i++)\r
+ {\r
+ if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)\r
+ {\r
+ *outSession = &client->olmSessions[i];\r
+ return true;\r
+ }\r
+ }\r
+\r
+ if (client->numOlmSessions < NUM_OLM_SESSIONS)\r
+ {\r
+ static char deviceKey[DEVICE_KEY_SIZE];\r
+ MatrixClientRequestDeviceKey(client,\r
+ deviceId,\r
+ deviceKey, DEVICE_KEY_SIZE);\r
+\r
+ MatrixOlmSessionFrom(\r
+ &client->olmSessions[client->numOlmSessions],\r
+ client->olmAccount.account,\r
+ deviceId,\r
+ deviceKey,\r
+ encrypted);\r
+\r
+ *outSession = &client->olmSessions[client->numOlmSessions];\r
+ \r
+ client->numOlmSessions++;\r
+\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientGetOlmSessionOut(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ MatrixOlmSession ** outSession)\r
+{\r
+ for (int i = 0; i < client->numOlmSessions; i++)\r
+ {\r
+ if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)\r
+ {\r
+ *outSession = &client->olmSessions[i];\r
+ return true;\r
+ }\r
+ }\r
+\r
+ if (client->numOlmSessions < NUM_OLM_SESSIONS)\r
+ {\r
+ static char deviceKey[DEVICE_KEY_SIZE];\r
+ MatrixClientRequestDeviceKey(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
+ MatrixOlmSessionTo(\r
+ &client->olmSessions[client->numOlmSessions],\r
+ client->olmAccount.account,\r
+ deviceId,\r
+ deviceKey,\r
+ onetimeKey);\r
+\r
+ *outSession = &client->olmSessions[client->numOlmSessions];\r
+ \r
+ client->numOlmSessions++;\r
+\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+// https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid\r
+bool\r
+MatrixClientSendToDevice(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ const char * message,\r
+ const char * msgType)\r
+{\r
+ static char requestUrl[MAX_URL_LEN];\r
+ sprintf(requestUrl,\r
+ TODEVICE_URL, msgType, (int)time(NULL));\r
+\r
+ static char eventBuffer[TODEVICE_EVENT_SIZE];\r
+ snprintf(eventBuffer, TODEVICE_EVENT_SIZE,\r
+ "{"\r
+ "\"messages\":{"\r
+ "\"%s\":{"\r
+ "\"%s\":%s"\r
+ "}"\r
+ "}"\r
+ "}",\r
+ userId,\r
+ deviceId,\r
+ message);\r
+\r
+ static char responseBuffer[ROOM_SEND_RESPONSE_SIZE];\r
+ bool result =\r
+ MatrixHttpPut(client,\r
+ requestUrl,\r
+ eventBuffer,\r
+ responseBuffer, ROOM_SEND_RESPONSE_SIZE,\r
+ true);\r
+ \r
+ printf("%s\n", responseBuffer);\r
+ \r
+ return result;\r
+}\r
+\r
+bool\r
+MatrixClientSendToDeviceEncrypted(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ const char * message,\r
+ const char * msgType)\r
+{\r
+ // get olm session\r
+ MatrixOlmSession * olmSession;\r
+ MatrixClientGetOlmSessionOut(client, userId, deviceId, &olmSession);\r
+\r
+ // create event json\r
+ char targetDeviceKey[DEVICE_KEY_SIZE];\r
+ MatrixClientRequestDeviceKey(client, deviceId, targetDeviceKey, DEVICE_KEY_SIZE);\r
+ char targetSigningKey[SIGNING_KEY_SIZE];\r
+ MatrixClientRequestSigningKey(client, deviceId, targetSigningKey, SIGNING_KEY_SIZE);\r
+ \r
+ char thisSigningKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
+\r
+ static char eventBuffer[TODEVICE_EVENT_SIZE];\r
+ sprintf(eventBuffer,\r
+ "{"\r
+ "\"type\":\"%s\","\r
+ "\"content\":%s,"\r
+ "\"sender\":\"%s\","\r
+ "\"recipient\":\"%s\","\r
+ "\"recipient_keys\":{"\r
+ "\"ed25519\":\"%s\""\r
+ "},"\r
+ "\"keys\":{"\r
+ "\"ed25519\":\"%s\""\r
+ "}"\r
+ "}",\r
+ msgType,\r
+ message,\r
+ client->userId,\r
+ userId, // recipient user id\r
+ targetSigningKey, // recipient device key\r
+ thisSigningKey);\r
+\r
+ // encrypt\r
+ static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];\r
+ MatrixOlmSessionEncrypt(olmSession,\r
+ eventBuffer,\r
+ encryptedBuffer, ENCRYPTED_REQUEST_SIZE);\r
+\r
+ char thisDeviceKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
+\r
+\r
+ static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];\r
+ sprintf(encryptedEventBuffer,\r
+ "{"\r
+ "\"algorithm\":\"m.olm.v1.curve25519-aes-sha2\","\r
+ "\"ciphertext\":{"\r
+ "\"%s\":{"\r
+ "\"body\":\"%s\","\r
+ "\"type\":%d"\r
+ "}"\r
+ "},"\r
+ "\"device_id\":\"%s\","\r
+ "\"sender_key\":\"%s\""\r
+ "}",\r
+ targetDeviceKey,\r
+ encryptedBuffer,\r
+ olm_session_has_received_message(olmSession->session),\r
+ client->deviceId,\r
+ thisDeviceKey);\r
+\r
+ // send\r
+ return MatrixClientSendToDevice(\r
+ client,\r
+ userId,\r
+ deviceId,\r
+ encryptedEventBuffer,\r
+ "m.room.encrypted");\r
+}\r
+\r
+bool\r
+MatrixClientSendDummy(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId)\r
+{\r
+ return MatrixClientSendToDeviceEncrypted(\r
+ client,\r
+ userId,\r
+ deviceId,\r
+ "{}",\r
+ "m.dummy");\r
+}\r
+\r
+bool\r
+MatrixClientFindDevice(\r
+ MatrixClient * client,\r
+ const char * deviceId,\r
+ MatrixDevice ** outDevice)\r
+{\r
+ MatrixClientRequestDeviceKeys(client);\r
+\r
+ for (int i = 0; i < client->numDevices; i++)\r
+ {\r
+ if (strcmp(client->devices[i].deviceId, deviceId) == 0)\r
+ {\r
+ *outDevice = &client->devices[i];\r
+ return true;\r
+ }\r
+ }\r
+\r
+ *outDevice = NULL;\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientRequestDeviceKey(\r
+ MatrixClient * client,\r
+ const char * deviceId,\r
+ char * outDeviceKey, int outDeviceKeyCap)\r
+{\r
+ MatrixDevice * device;\r
+ \r
+ if (MatrixClientFindDevice(client, deviceId, &device))\r
+ {\r
+ strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);\r
+ return true;\r
+ }\r
+\r
+ MatrixClientRequestDeviceKeys(client);\r
+ \r
+ if (MatrixClientFindDevice(client, deviceId, &device))\r
+ {\r
+ strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientRequestSigningKey(\r
+ MatrixClient * client,\r
+ const char * deviceId,\r
+ char * outSigningKey, int outSigningKeyCap)\r
+{\r
+ MatrixDevice * device;\r
+ \r
+ if (MatrixClientFindDevice(client, deviceId, &device))\r
+ {\r
+ strncpy(outSigningKey, device->signingKey, outSigningKeyCap);\r
+ return true;\r
+ }\r
+\r
+ MatrixClientRequestDeviceKeys(client);\r
+ \r
+ if (MatrixClientFindDevice(client, deviceId, &device))\r
+ {\r
+ strncpy(outSigningKey, device->signingKey, outSigningKeyCap);\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+// https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3keysquery\r
+bool\r
+MatrixClientRequestDeviceKeys(\r
+ MatrixClient * client)\r
+{\r
+ static char userIdEscaped[USER_ID_SIZE];\r
+ JsonEscape(client->userId, strlen(client->userId),\r
+ userIdEscaped, USER_ID_SIZE);\r
+\r
+ static char request[KEYS_QUERY_REQUEST_SIZE];\r
+ snprintf(request, KEYS_QUERY_REQUEST_SIZE,\r
+ "{\"device_keys\":{\"%s\":[]}}", client->userId);\r
+\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
+ return false;\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
+ // 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
+ // look for signing key in value\r
+ static char signingKeyQuery[JSON_QUERY_SIZE];\r
+ snprintf(signingKeyQuery, JSON_QUERY_SIZE,\r
+ "$.keys.ed25519:%s", d.deviceId);\r
+ mjson_get_string(val, vlen,\r
+ signingKeyQuery, d.signingKey, SIGNING_KEY_SIZE);\r
+\r
+ // add device\r
+ if (client->numDevices < NUM_DEVICES)\r
+ {\r
+ bool foundDevice = false;\r
+ for (int i = 0; i < client->numDevices; i++)\r
+ if (strcmp(client->devices[i].deviceId, d.deviceId) == 0)\r
+ foundDevice = true;\r
+\r
+ if (! foundDevice) {\r
+ printf("new device: %s %s %s\n", d.deviceId, d.deviceKey, d.signingKey);\r
+ client->devices[client->numDevices] = d;\r
+ client->numDevices++;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientDeleteDevice(\r
+ MatrixClient * client)\r
+{\r
+ static char deleteRequest[1024];\r
+ snprintf(deleteRequest, 1024,\r
+ "{\"devices\":[\"%s\"]}",\r
+ client->deviceId);\r
+ static char deleteResponse[1024];\r
+ bool res = MatrixHttpPost(client, "/_matrix/client/v3/delete_devices",\r
+ deleteRequest, deleteResponse, 1024, true);\r
+ return res;\r
+}
\ No newline at end of file