\r
#define ENCRYPTED_REQUEST_SIZE (1024*5)\r
#define ENCRYPTED_EVENT_SIZE (1024*10)\r
-#define ROOMEVENT_REQUEST_SIZE 256\r
-#define ROOMEVENT_RESPONSE_SIZE 1024\r
-#define ROOMEVENT_URL "/_matrix/client/v3/rooms/%s/send/%s/%d"\r
+#define ROOM_SEND_REQUEST_SIZE 256\r
+#define ROOM_SEND_RESPONSE_SIZE 1024\r
+#define ROOM_SEND_URL "/_matrix/client/v3/rooms/%s/send/%s/%d"\r
+\r
+#define ROOMKEY_REQUEST_SIZE (1024*4)\r
\r
#define TODEVICE_EVENT_SIZE (1024*5)\r
#define TODEVICE_URL "/_matrix/client/v3/sendToDevice/%s/%d"\r
\r
int signatureLen = res;\r
\r
- char thisSigningKey[DEVICE_KEY_SIZE];\r
- MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
+ static char thisSigningKey[SIGNING_KEY_SIZE];\r
+ MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, SIGNING_KEY_SIZE);\r
\r
static char signatureJson[JSON_SIGNATURE_SIZE];\r
int signatureJsonLen =\r
"}"\r
"}",\r
client->userId,\r
- "1",\r
+ //"1",\r
+ client->deviceId,\r
signatureLen, signature);\r
\r
struct mjson_fixedbuf result = { sOut, sOutCap, 0 };\r
return res != olm_error();\r
}\r
\r
+bool\r
+MatrixOlmSessionDecrypt(\r
+ MatrixOlmSession * session,\r
+ size_t messageType,\r
+ char * encrypted,\r
+ char * outBuffer, int outBufferCap)\r
+{\r
+ static uint8_t random[OLM_ENCRYPT_RANDOM_SIZE];\r
+ Randomize(random, OLM_ENCRYPT_RANDOM_SIZE);\r
+\r
+ size_t res =\r
+ olm_decrypt(session->session,\r
+ messageType,\r
+ encrypted, strlen(encrypted),\r
+ outBuffer, outBufferCap);\r
+ \r
+ if (res != olm_error() && res < outBufferCap)\r
+ outBuffer[outBufferCap] = '\0';\r
+\r
+ return res != olm_error();\r
+}\r
+\r
// https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session\r
bool\r
MatrixMegolmOutSessionInit(\r
static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
\r
mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
- "{\"one_time_keys\":{");\r
+ "{");\r
\r
static char onetimeKeysBuffer[1024];\r
olm_account_one_time_keys(client->olmAccount.account,\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
+ int keyJsonLen =\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
+ keyJson, keyJsonLen,\r
keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);\r
\r
mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
keyJsonSigned);\r
}\r
\r
- mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
- "}}");\r
+ if (requestBuffer[strlen(requestBuffer)-1] == ',')\r
+ requestBuffer[strlen(requestBuffer)-1] = '\0';\r
+\r
+ mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
+ "}");\r
+ \r
+ // static char onetimeKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ // JsonSign(client,\r
+ // requestBuffer, strlen(requestBuffer),\r
+ // onetimeKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
+ \r
+ // static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ // snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+ // "{\"one_time_keys\":%s}", onetimeKeysSignedBuffer);\r
+ static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+ "{\"one_time_keys\":%s}", requestBuffer);\r
\r
static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
MatrixHttpPost(client,\r
KEYS_UPLOAD_URL,\r
- requestBuffer,\r
+ finalEvent,\r
responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
true);\r
\r
+ printf("%s\n", responseBuffer);\r
+\r
return true;\r
}\r
\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, thisDeviceKey,\r
- client->deviceId, thisSigningKey,\r
- client->userId);\r
+ int deviceKeysBufferLen =\r
+ mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+ "{"\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, thisDeviceKey,\r
+ client->deviceId, thisSigningKey,\r
+ client->userId);\r
\r
static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
JsonSign(client,\r
- deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+ deviceKeysBuffer, deviceKeysBufferLen,\r
deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
-\r
+ \r
+ static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+ "{\"device_keys\":%s}", deviceKeysSignedBuffer);\r
\r
static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
MatrixHttpPost(client,\r
KEYS_UPLOAD_URL,\r
- deviceKeysSignedBuffer,\r
+ finalEvent,\r
responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
true);\r
+ \r
+ printf("%s\n", responseBuffer);\r
\r
return true;\r
}\r
{ \r
static char requestUrl[MAX_URL_LEN];\r
sprintf(requestUrl,\r
- ROOMEVENT_URL, roomId, msgType, (int)time(NULL));\r
+ ROOM_SEND_URL, roomId, msgType, (int)time(NULL));\r
\r
- static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];\r
+ static char responseBuffer[ROOM_SEND_RESPONSE_SIZE];\r
bool result =\r
MatrixHttpPut(client,\r
requestUrl,\r
msgBody,\r
- responseBuffer, ROOMEVENT_RESPONSE_SIZE,\r
+ responseBuffer, ROOM_SEND_RESPONSE_SIZE,\r
true);\r
\r
return result;\r
const char * msgBody)\r
{\r
// event json\r
- static char requestBuffer[ROOMEVENT_REQUEST_SIZE];\r
+ static char requestBuffer[ROOM_SEND_REQUEST_SIZE];\r
sprintf(requestBuffer,\r
"{"\r
"\"type\":\"%s\","\r
bool\r
MatrixClientSync(\r
MatrixClient * client,\r
- char * outSyncBuffer, int outSyncCap)\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%s",\r
+ strlen(nextBatch) > 0 ? "?since=" : "");\r
+ \r
+ int index = strlen(url);\r
+\r
+ for (int 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
- "/_matrix/client/v3/sync",\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
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
+ MatrixMegolmInSession * outMegolmInSession)\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
MatrixClientGetOlmSession(\r
MatrixClient * client,\r
deviceId,\r
message);\r
\r
- static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];\r
+ static char responseBuffer[ROOM_SEND_RESPONSE_SIZE];\r
bool result =\r
MatrixHttpPut(client,\r
requestUrl,\r
eventBuffer,\r
- responseBuffer, ROOMEVENT_RESPONSE_SIZE,\r
+ responseBuffer, ROOM_SEND_RESPONSE_SIZE,\r
true);\r
\r
+ printf("%s\n", responseBuffer);\r
+ \r
return result;\r
}\r
\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
if (! requestResult)\r
return false;\r
\r
+ printf("keys:\n%s\n", responseBuffer);\r
+\r
// query for retrieving device keys for user id\r
static char query[JSON_QUERY_SIZE];\r
snprintf(query, JSON_QUERY_SIZE,\r