From 30bde47d1d5d9b6f0b59c318ff16caed6268d1a8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 5 Sep 2023 16:01:57 +0200 Subject: [PATCH] verify device --- Todo.md | 6 +- esp32/esp_project/main/main.c | 36 +++- examples/Decrypt.c | 326 ++++++++++++++++++++++++++++++++-- examples/Decrypt.rdbg | Bin 0 -> 1074 bytes src/matrix.c | 204 +++++++++++++++++---- src/matrix.h | 36 ++++ 6 files changed, 546 insertions(+), 62 deletions(-) create mode 100644 examples/Decrypt.rdbg diff --git a/Todo.md b/Todo.md index 6698a4c..23734f7 100644 --- a/Todo.md +++ b/Todo.md @@ -3,16 +3,18 @@ + add esp build - (write script to build dependencies) - automate script to generate header with size defines +- run on esp # Matrix Lib + manage keys + upload keys -- create olm session - - incoming ++ create olm session + + incoming + outgoing + store keys/sessions - respond to events - room_key_request + add client saving/loading +- overhaul client saving/loading + esp compatibility - http requests in chunks/dynamically allocated \ No newline at end of file diff --git a/esp32/esp_project/main/main.c b/esp32/esp_project/main/main.c index f4d3c8c..b090849 100644 --- a/esp32/esp_project/main/main.c +++ b/esp32/esp_project/main/main.c @@ -17,9 +17,8 @@ #include #define SERVER "https://matrix.org" -#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO" -#define DEVICE_ID "MAZNCCZLBR" -#define ROOM_ID "!koVStwyiiKcBVbXZYz:matrix.org" +#define USER_ID "@pscho:matrix.org" +#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" void app_main(void) @@ -28,18 +27,37 @@ app_main(void) MatrixClientInit(&client, SERVER); - void wifi_init(const char *ssid, const char *pass); - wifi_init("Hundehuette", "Affensicherespw55"); - MatrixHttpInit(&client); - MatrixClientSetAccessToken(&client, - ACCESS_TOKEN); + MatrixClientSetUserId(&client, USER_ID); + + MatrixClientLoginPassword(&client, + "pscho", + "Wc23EbmB9G3faMq", + "Test1"); - MatrixClientSendEvent(&client, + MatrixClientUploadDeviceKey(&client); + MatrixClientGenerateOnetimeKeys(&client, 10); + MatrixClientUploadOnetimeKeys(&client); + + // create megolmsession + MatrixMegolmOutSession * megolmOutSession; + MatrixClientGetMegolmOutSession(&client, + ROOM_ID, + &megolmOutSession); + printf("megolm session id: %.10s... key: %.10s...\n", megolmOutSession->id, megolmOutSession->key); + + MatrixClientShareMegolmOutSession(&client, + USER_ID, + "ULZZOKJBYN", + megolmOutSession); + + MatrixClientSendEventEncrypted(&client, ROOM_ID, "m.room.message", "{\"body\":\"Hello\",\"msgtype\":\"m.text\"}"); + MatrixClientDeleteDevice(&client); + MatrixHttpDeinit(&client); } diff --git a/examples/Decrypt.c b/examples/Decrypt.c index b496780..fe305cc 100644 --- a/examples/Decrypt.c +++ b/examples/Decrypt.c @@ -1,11 +1,26 @@ #include #include +#include + #define SERVER "https://matrix.org" -#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO" -#define DEVICE_ID "MAZNCCZLBR" -#define ROOM_ID "!koVStwyiiKcBVbXZYz:matrix.org" -#define EVENT_ID "" +#define USER_ID "@pscho:matrix.org" +#define DEVICE_ID "ULZZOKJBYN" +#define SENDER_KEY "LvVdoHsRRWNxRmG2GO2vky6o6S8RzADpPAaELsd1rjU" +#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" +#define EVENT_ID "$_-y42DkC3OmJ_s40gYko7jMwrUQhoXfEut2pMV3E2J8" +#define SESSION_ID "tzdnJbDrm82D/RpgkZKpILTifQ5Rads+tVzp3ax8+Ls" + +void +GetLine(char * buffer, int n) { + int c; + int len = 0; + + while ((c = getchar()) != '\n' && len < n-1) + buffer[len++] = c; + + buffer[len] = '\0'; +} int main(void) @@ -16,30 +31,313 @@ main(void) MatrixHttpInit(&client); - MatrixClientSetAccessToken(&client, - ACCESS_TOKEN); + MatrixClientSetUserId(&client, USER_ID); + + MatrixClientLoginPassword(&client, + "pscho", + "Wc23EbmB9G3faMq", + "Test1"); + + printf("deviceId: %s\n", client.deviceId); + + MatrixClientGenerateOnetimeKeys(&client, 10); + MatrixClientUploadOnetimeKeys(&client); + MatrixClientUploadDeviceKey(&client); static char eventBuffer[1024]; MatrixClientGetRoomEvent(&client, ROOM_ID, EVENT_ID, eventBuffer, 1024); + + printf("event: %s\n", eventBuffer); + + // verify + char theirDeviceKey[DEVICE_KEY_SIZE]; + MatrixClientRequestDeviceKey(&client, + DEVICE_ID, + theirDeviceKey, DEVICE_KEY_SIZE); + + char transactionId[256]; + GetLine(transactionId, 128); + + char verificationReadyBuffer[2048]; + snprintf(verificationReadyBuffer, 2048, + "{" + "\"from_device\":\"%s\"," + "\"methods\":[\"m.sas.v1\"]," + "\"transaction_id\":\"%s\"" + "}", + client.deviceId, + transactionId); + + MatrixClientSendToDevice(&client, + USER_ID, + DEVICE_ID, + verificationReadyBuffer, + "m.key.verification.ready"); + + OlmSAS * olmSas = olm_sas(malloc(olm_sas_size())); + void * sasRandomBytes = malloc(olm_create_sas_random_length(olmSas)); + olm_create_sas(olmSas, + sasRandomBytes, + olm_create_sas_random_length(olmSas)); + + OlmUtility * olmUtil = olm_utility(malloc(olm_utility_size())); + + char publicKey[128]; + char keyStartJson[1024]; + char concat[1024]; + char commitment[256]; + olm_sas_get_pubkey(olmSas, + publicKey, + 128); + GetLine(keyStartJson, 1024); + printf("keyStartJson: %s\n", keyStartJson); + int concatLen = + snprintf(concat, 1024, "%s%s", publicKey, keyStartJson); + printf("concat: %s\n", concat); + olm_sha256(olmUtil, concat, strlen(concat), commitment, 256); + printf("hash: %s\n", commitment); + + char verificationAcceptBuffer[2048]; + snprintf(verificationAcceptBuffer, 2048, + "{" + "\"commitment\":\"%s\"," + "\"hash\":\"sha256\"," + "\"key_agreement_protocol\":\"curve25519\"," + "\"message_authentication_code\":\"hkdf-hmac-sha256.v2\"," + "\"method\":\"m.sas.v1\"," + "\"short_authentication_string\":[\"decimal\"]," + "\"transaction_id\":\"%s\"" + "}", + commitment, + transactionId); + + MatrixClientSendToDevice(&client, + USER_ID, + DEVICE_ID, + verificationAcceptBuffer, + "m.key.verification.accept"); + + char theirPublicKey[128]; + GetLine(theirPublicKey, 128); + olm_sas_set_their_key(olmSas, theirPublicKey, strlen(theirPublicKey)); + + char verificationKeyBuffer[2048]; + snprintf(verificationKeyBuffer, 2048, + "{" + "\"key\":\"%s\"," + "\"transaction_id\":\"%s\"" + "}", + publicKey, + transactionId); + + MatrixClientSendToDevice(&client, + USER_ID, + DEVICE_ID, + verificationKeyBuffer, + "m.key.verification.key"); + + char hkdfInfo[1024]; + int hkdfInfoLen = + snprintf(hkdfInfo, 1024, + "MATRIX_KEY_VERIFICATION_SAS%s%s%s%s%s", + USER_ID, + DEVICE_ID, + USER_ID, + client.deviceId, + transactionId); + + unsigned char sasBytes[5]; + olm_sas_generate_bytes(olmSas, + hkdfInfo, hkdfInfoLen, + sasBytes, 5); + int b0 = sasBytes[0]; + int b1 = sasBytes[1]; + int b2 = sasBytes[2]; + int b3 = sasBytes[3]; + int b4 = sasBytes[4]; + + printf("%d %d %d %d %d\n", b0, b1, b2, b3, b4); + // https://spec.matrix.org/v1.7/client-server-api/#sas-method-decimal + printf("%d | %d | %d\n", + (b0 << 5 | b1 >> 3) + 1000, + ((b1 & 0x7) << 10 | b2 << 2 | b3 >> 6) + 1000, + ((b3 & 0x3F) << 7 | b4 >> 1) + 1000); + printf("%d | %d | %d\n", + ((b0 << 5) | (b1 >> 3)) + 1000, + (((b1 & 0x7) << 10) | (b2 << 2) | (b3 >> 6)) + 1000, + (((b3 & 0x3F) << 7) | (b4 >> 1)) + 1000); + + // mac + const char * masterKey = "vt8tJ5/SxqkvXS+XoGxr+4rJNe8fJfZT3/e/FTwlFsI"; + + char keyList[1024]; + char keyListMac[1024]; + char key1Id[1024]; + char key1[1024]; + char key1Mac[1024]; + char key2Id[1024]; + char key2[1024]; + char key2Mac[1024]; + + if (strcmp(masterKey, client.deviceId) < 0) { + //strcpy(key1Id, masterKey); + snprintf(key1Id, 1024, "ed25519:%s", masterKey); + strcpy(key1, masterKey); + //strcpy(key2Id, client.deviceId); + snprintf(key2Id, 1024, "ed25519:%s", client.deviceId); + MatrixOlmAccountGetSigningKey(&client.olmAccount, key2, 1024); + } + else { + //strcpy(key1Id, client.deviceId); + snprintf(key1Id, 1024, "ed25519:%s", client.deviceId); + MatrixOlmAccountGetSigningKey(&client.olmAccount, key1, 1024); + //strcpy(key2Id, masterKey); + snprintf(key2Id, 1024, "ed25519:%s", masterKey); + strcpy(key2, masterKey); + } + + snprintf(keyList, 1024, + "%s,%s", key1Id, key2Id); + + char macInfo[1024]; + int macInfoLen; + { + macInfoLen = + snprintf(macInfo, 1024, + "MATRIX_KEY_VERIFICATION_MAC%s%s%s%s%s%s", + USER_ID, + client.deviceId, + USER_ID, + DEVICE_ID, + transactionId, + "KEY_IDS"); + olm_sas_calculate_mac_fixed_base64(olmSas, keyList, strlen(keyList), macInfo, macInfoLen, keyListMac, 1024); + } + { + macInfoLen = + snprintf(macInfo, 1024, + "MATRIX_KEY_VERIFICATION_MAC%s%s%s%s%s%s", + USER_ID, + client.deviceId, + USER_ID, + DEVICE_ID, + transactionId, + key1Id); + olm_sas_calculate_mac_fixed_base64(olmSas, key1, strlen(key1), macInfo, macInfoLen, key1Mac, 1024); + } + { + macInfoLen = + snprintf(macInfo, 1024, + "MATRIX_KEY_VERIFICATION_MAC%s%s%s%s%s%s", + USER_ID, + client.deviceId, + USER_ID, + DEVICE_ID, + transactionId, + key2Id); + olm_sas_calculate_mac_fixed_base64(olmSas, key2, strlen(key2), macInfo, macInfoLen, key2Mac, 1024); + } + + printf("send mac:"); + getchar(); + + char verificationMacBuffer[2048]; + snprintf(verificationMacBuffer, 2048, + "{" + "\"keys\":\"%s\"," + "\"mac\":{" + "\"%s\":\"%s\"," + "\"%s\":\"%s\"" + "}," + "\"transaction_id\":\"%s\"" + "}", + keyListMac, + key1Id, + key1Mac, + key2Id, + key2Mac, + transactionId); + + MatrixClientSendToDevice(&client, + USER_ID, + DEVICE_ID, + verificationMacBuffer, + "m.key.verification.mac"); + + printf("send done:"); + getchar(); + + char verificationDoneBuffer[2048]; + snprintf(verificationDoneBuffer, 2048, + "{" + "\"transaction_id\":\"%s\"" + "}", + transactionId); + + MatrixClientSendToDevice(&client, + USER_ID, + DEVICE_ID, + verificationDoneBuffer, + "m.key.verification.done"); + + // done + + // request room key + + getchar(); + MatrixMegolmInSession megolmSession; - MatrixClientRequestMegolmSession(&client, + MatrixClientRequestMegolmInSession(&client, ROOM_ID, - EVENT_ID, + SESSION_ID, + SENDER_KEY, + USER_ID, + DEVICE_ID, &megolmSession); - static char decryptedBuffer[1024]; - MatrixMegolmSessionDecrypt(&megolmSession, - eventBuffer, - decryptedBuffer, 1024); + // // decrypt room key + + MatrixOlmSession * olmSession; + MatrixClientGetOlmSession(&client, + USER_ID, + DEVICE_ID, + &olmSession); + static char encrypted[2048]; + static char decrypted[2048]; + printf("encrypted:"); + gets(encrypted); + printf("(%d) %s;\n", strlen(encrypted), encrypted); + MatrixOlmSessionDecrypt(olmSession, + 1, encrypted, decrypted, 2048); + printf("decrypted: %s\n", decrypted); + + // int c; + // while ((c = getchar()) != 'q') { + // printf("c: %c (%d)\n", c, c); + // static char syncBuffer[40000]; + // MatrixClientSync(&client, + // syncBuffer, 40000); + // printf("sync: %s", syncBuffer); + // } + + + // static char decryptedBuffer[1024]; + // MatrixMegolmInSessionDecrypt(&megolmSession, + // eventBuffer, + // decryptedBuffer, 1024); + + // printf("%s\n", decryptedBuffer); + + getchar(); - printf("%s\n", decryptedBuffer); + MatrixClientDeleteDevice(&client); MatrixHttpDeinit(&client); return 0; -} \ No newline at end of file +} diff --git a/examples/Decrypt.rdbg b/examples/Decrypt.rdbg new file mode 100644 index 0000000000000000000000000000000000000000..4e8358317ab5bdafe79c6baef435b3e3bf73ca26 GIT binary patch literal 1074 zcmWG?adP)!U|?{ziU}=FEh>)5&acSJk8w#Y&MwI>h{;VXE=etl@l7l#%B*nC$xO{F z@ySdoN-V03$uBL5Nv%lCEyzhNj&Vs%E~+dj(MzpJWq<-!1_lNfvP{M>HQAL+b4p_J zb8=&fi;_XKUUET!9a#o}49EosfnKr`Sth`}t4z|-10PclMVCHw~jXKATg*XP$^J!f<)Ls1Re}B0E8JC b85u#&#-kKP3S>Sb2h^`9GWaolmAccount, thisSigningKey, DEVICE_KEY_SIZE); + static char thisSigningKey[SIGNING_KEY_SIZE]; + MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, SIGNING_KEY_SIZE); static char signatureJson[JSON_SIGNATURE_SIZE]; int signatureJsonLen = @@ -103,7 +105,8 @@ bool JsonSign( "}" "}", client->userId, - "1", + //"1", + client->deviceId, signatureLen, signature); struct mjson_fixedbuf result = { sOut, sOutCap, 0 }; @@ -257,6 +260,28 @@ MatrixOlmSessionEncrypt( return res != olm_error(); } +bool +MatrixOlmSessionDecrypt( + MatrixOlmSession * session, + size_t messageType, + char * encrypted, + char * outBuffer, int outBufferCap) +{ + static uint8_t random[OLM_ENCRYPT_RANDOM_SIZE]; + Randomize(random, OLM_ENCRYPT_RANDOM_SIZE); + + size_t res = + olm_decrypt(session->session, + messageType, + encrypted, strlen(encrypted), + outBuffer, outBufferCap); + + if (res != olm_error() && res < outBufferCap) + outBuffer[outBufferCap] = '\0'; + + return res != olm_error(); +} + // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session bool MatrixMegolmOutSessionInit( @@ -510,7 +535,7 @@ MatrixClientUploadOnetimeKeys( static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE]; mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE, - "{\"one_time_keys\":{"); + "{"); static char onetimeKeysBuffer[1024]; olm_account_one_time_keys(client->olmAccount.account, @@ -524,14 +549,15 @@ MatrixClientUploadOnetimeKeys( while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) { static char keyJson[JSON_ONETIME_KEY_SIZE]; - snprintf(keyJson, JSON_ONETIME_KEY_SIZE, - "{\"key\":\"%.*s\"}", - vlen-2, keys + voff+1); + int keyJsonLen = + snprintf(keyJson, JSON_ONETIME_KEY_SIZE, + "{\"key\":\"%.*s\"}", + vlen-2, keys + voff+1); static char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE]; JsonSign(client, - keyJson, JSON_ONETIME_KEY_SIZE, + keyJson, keyJsonLen, keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE); mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer), @@ -540,16 +566,33 @@ MatrixClientUploadOnetimeKeys( keyJsonSigned); } - mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer), - "}}"); + if (requestBuffer[strlen(requestBuffer)-1] == ',') + requestBuffer[strlen(requestBuffer)-1] = '\0'; + + mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer), + "}"); + + // static char onetimeKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE]; + // JsonSign(client, + // requestBuffer, strlen(requestBuffer), + // onetimeKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE); + + // static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE]; + // snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE, + // "{\"one_time_keys\":%s}", onetimeKeysSignedBuffer); + static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE]; + snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE, + "{\"one_time_keys\":%s}", requestBuffer); static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE]; MatrixHttpPost(client, KEYS_UPLOAD_URL, - requestBuffer, + finalEvent, responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE, true); + printf("%s\n", responseBuffer); + return true; } @@ -565,33 +608,39 @@ MatrixClientUploadDeviceKey( static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE]; - mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE, - "{\"device_keys\":{" - "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"]," - "\"device_id\":\"%s\"," - "\"keys\":{" - "\"curve25519:%s\":\"%s\"," - "\"ed25519:%s\":\"%s\"" - "}," - "\"user_id\":\"%s\"" - "}}", - client->deviceId, - client->deviceId, thisDeviceKey, - client->deviceId, thisSigningKey, - client->userId); + int deviceKeysBufferLen = + mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE, + "{" + "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"]," + "\"device_id\":\"%s\"," + "\"keys\":{" + "\"curve25519:%s\":\"%s\"," + "\"ed25519:%s\":\"%s\"" + "}," + "\"user_id\":\"%s\"" + "}", + client->deviceId, + client->deviceId, thisDeviceKey, + client->deviceId, thisSigningKey, + client->userId); static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE]; JsonSign(client, - deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE, + deviceKeysBuffer, deviceKeysBufferLen, deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE); - + + static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE]; + snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE, + "{\"device_keys\":%s}", deviceKeysSignedBuffer); static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE]; MatrixHttpPost(client, KEYS_UPLOAD_URL, - deviceKeysSignedBuffer, + finalEvent, responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE, true); + + printf("%s\n", responseBuffer); return true; } @@ -715,14 +764,14 @@ MatrixClientSendEvent( { static char requestUrl[MAX_URL_LEN]; sprintf(requestUrl, - ROOMEVENT_URL, roomId, msgType, (int)time(NULL)); + ROOM_SEND_URL, roomId, msgType, (int)time(NULL)); - static char responseBuffer[ROOMEVENT_RESPONSE_SIZE]; + static char responseBuffer[ROOM_SEND_RESPONSE_SIZE]; bool result = MatrixHttpPut(client, requestUrl, msgBody, - responseBuffer, ROOMEVENT_RESPONSE_SIZE, + responseBuffer, ROOM_SEND_RESPONSE_SIZE, true); return result; @@ -738,7 +787,7 @@ MatrixClientSendEventEncrypted( const char * msgBody) { // event json - static char requestBuffer[ROOMEVENT_REQUEST_SIZE]; + static char requestBuffer[ROOM_SEND_REQUEST_SIZE]; sprintf(requestBuffer, "{" "\"type\":\"%s\"," @@ -802,6 +851,27 @@ MatrixClientSync( true); } +// https://spec.matrix.org/v1.7/client-server-api/#get_matrixclientv3roomsroomideventeventid +bool +MatrixClientGetRoomEvent( + MatrixClient * client, + const char * roomId, + const char * eventId, + char * outEvent, int outEventCap) +{ + static char url[MAX_URL_LEN]; + snprintf(url, MAX_URL_LEN, + "/_matrix/client/v3/rooms/%s/event/%s", + roomId, + eventId); + + return + MatrixHttpGet(client, + url, + outEvent, outEventCap, + true); +} + bool MatrixClientShareMegolmOutSession( MatrixClient * client, @@ -932,6 +1002,48 @@ MatrixClientInitMegolmOutSession( return false; } +bool +MatrixClientRequestMegolmInSession( + MatrixClient * client, + const char * roomId, + const char * sessionId, + const char * senderKey, + const char * userId, + const char * deviceId, + MatrixMegolmInSession * outMegolmInSession) +{ + // TODO: cancel requests + MatrixClientSendDummy(client, userId, deviceId); + + static char event[ROOMKEY_REQUEST_SIZE]; + snprintf(event, ROOMKEY_REQUEST_SIZE, + "{" + "\"action\":\"request\"," + "\"body\":{" + "\"algorithm\":\"m.megolm.v1.aes-sha2\"," + "\"room_id\":\"%s\"," + "\"sender_key\":\"%s\"," + "\"session_id\":\"%s\"" + "}," + "\"request_id\":\"%d\"," + "\"requesting_device_id\":\"%s\"" + "}", + roomId, + senderKey, + sessionId, + time(NULL), + client->deviceId); + + + MatrixClientSendToDevice(client, + userId, + deviceId, + event, + "m.room_key_request"); + + return true; +} + bool MatrixClientGetOlmSession( MatrixClient * client, @@ -1004,14 +1116,16 @@ MatrixClientSendToDevice( deviceId, message); - static char responseBuffer[ROOMEVENT_RESPONSE_SIZE]; + static char responseBuffer[ROOM_SEND_RESPONSE_SIZE]; bool result = MatrixHttpPut(client, requestUrl, eventBuffer, - responseBuffer, ROOMEVENT_RESPONSE_SIZE, + responseBuffer, ROOM_SEND_RESPONSE_SIZE, true); + printf("%s\n", responseBuffer); + return result; } @@ -1097,6 +1211,20 @@ MatrixClientSendToDeviceEncrypted( "m.room.encrypted"); } +bool +MatrixClientSendDummy( + MatrixClient * client, + const char * userId, + const char * deviceId) +{ + return MatrixClientSendToDeviceEncrypted( + client, + userId, + deviceId, + "{}", + "m.dummy"); +} + bool MatrixClientFindDevice( MatrixClient * client, @@ -1191,6 +1319,8 @@ MatrixClientRequestDeviceKeys( if (! requestResult) return false; + printf("keys:\n%s\n", responseBuffer); + // query for retrieving device keys for user id static char query[JSON_QUERY_SIZE]; snprintf(query, JSON_QUERY_SIZE, diff --git a/src/matrix.h b/src/matrix.h index b51d0e9..20cdc8c 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -119,6 +119,13 @@ MatrixOlmSessionEncrypt( const char * plaintext, char * outBuffer, int outBufferCap); +bool +MatrixOlmSessionDecrypt( + MatrixOlmSession * session, + size_t messageType, + char * encrypted, + char * outBuffer, int outBufferCap); + // Matrix Megolm Session @@ -126,6 +133,12 @@ typedef struct MatrixMegolmInSession { OlmInboundGroupSession * session; } MatrixMegolmInSession; +bool +MatrixMegolmInSessionDecrypt( + MatrixMegolmInSession * megolmInSession, + const char * encrypted, + char * outDecrypted, int outDecryptedCap); + typedef struct MatrixMegolmOutSession { char roomId[ROOM_ID_SIZE]; @@ -264,6 +277,13 @@ MatrixClientSync( MatrixClient * client, char * outSync, int outSyncCap); +bool +MatrixClientGetRoomEvent( + MatrixClient * client, + const char * roomId, + const char * eventId, + char * outEvent, int outEventCap); + bool MatrixClientShareMegolmOutSession( MatrixClient * client, @@ -294,6 +314,16 @@ bool MatrixClientInitMegolmOutSession( MatrixClient * client, const char * roomId); + +bool +MatrixClientRequestMegolmInSession( + MatrixClient * client, + const char * roomId, + const char * sessionId, + const char * senderKey, + const char * userId, + const char * deviceId, // TODO: remove deviceId (query all devices) + MatrixMegolmInSession * outMegolmInSession); bool MatrixClientGetOlmSession( @@ -318,6 +348,12 @@ MatrixClientSendToDeviceEncrypted( const char * message, const char * msgType); +bool +MatrixClientSendDummy( + MatrixClient * client, + const char * userId, + const char * deviceId); + bool MatrixClientRequestDeviceKey( MatrixClient * client, -- 2.50.1