From: Patrick Date: Tue, 14 Nov 2023 12:12:41 +0000 (+0100) Subject: update examples X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/commitdiff_plain/be730e09b6376a40fd3852d5a2c8876b27c2c7bb?ds=sidebyside update examples --- diff --git a/examples/Cli.c b/examples/Cli.c index d4d8d22..bcd3a12 100644 --- a/examples/Cli.c +++ b/examples/Cli.c @@ -4,9 +4,11 @@ #include #include -#define SERVER "https://matrix.org" -#define USER_ID "@pscho:matrix.org" -#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" +#define SERVER "https://matrix.org" +#define USER_ID "@example:matrix.org" +#define USERNAME "" +#define PASSWORD "" +#define DEVICE_NAME "" #define BUFFER_SIZE 1024 #define NUMBER_ARGS 10 @@ -130,8 +132,9 @@ ExecuteCommand( } else if (CheckCommand(cmd, "todevice")) { static char buffer[30000]; + static char nextBatch[128]; MatrixClientSync(client, - buffer, 30000, ""); + buffer, 30000, nextBatch, 128); const char * todevice; int todeviceLen; mjson_find(buffer, 30000, @@ -151,16 +154,6 @@ ExecuteCommand( args[1], args[2]); } - else if (CheckCommand(cmd, "save")) { - CHECK_ARGS(1, "") - - MatrixClientSave(client, args[0]); - } - else if (CheckCommand(cmd, "load")) { - CHECK_ARGS(1, "") - - MatrixClientLoad(client, args[0]); - } else if (CheckCommand(cmd, "send")) { CHECK_ARGS(2, " ") @@ -218,26 +211,6 @@ ExecuteCommand( args[2], &client->megolmOutSessions[sessionIndex]); } - else if (CheckCommand(cmd, "savesession")) { - CHECK_ARGS(3, " ") - - int sessionIndex = atoi(args[0]); - - MatrixMegolmOutSessionSave( - &client->megolmOutSessions[sessionIndex], - args[1], - args[2]); - } - else if (CheckCommand(cmd, "loadsession")) { - CHECK_ARGS(3, " ") - - int sessionIndex = atoi(args[0]); - - MatrixMegolmOutSessionLoad( - &client->megolmOutSessions[sessionIndex], - args[1], - args[2]); - } else if (CheckCommand(cmd, "printsessions")) { for (int i = 0; i < client->numMegolmOutSessions; i++) { printf("%d: %s\t%s\t%s\n", i, @@ -277,7 +250,7 @@ main(void) MatrixClientSetUserId(&client, USER_ID); - MatrixClientLoginPassword(&client, "@pscho:matrix.org", "Wc23EbmB9G3faMq", "abc"); + MatrixClientLoginPassword(&client, USERNAME, PASSWORD, DEVICE_NAME); MatrixClientGenerateOnetimeKeys(&client, 10); MatrixClientUploadDeviceKeys(&client); MatrixClientUploadOnetimeKeys(&client); diff --git a/examples/Decrypt.c b/examples/Decrypt.c deleted file mode 100644 index 7923693..0000000 --- a/examples/Decrypt.c +++ /dev/null @@ -1,338 +0,0 @@ -#include -#include - -#include - -#define SERVER "https://matrix.org" -#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) -{ - MatrixClient client; - MatrixClientInit(&client); - - MatrixHttpInit(&client.hc, SERVER); - - MatrixClientSetUserId(&client, USER_ID); - - MatrixClientLoginPassword(&client, - "pscho", - "Wc23EbmB9G3faMq", - "Test1"); - - printf("deviceId: %s\n", client.deviceId); - - MatrixClientGenerateOnetimeKeys(&client, 10); - MatrixClientUploadOnetimeKeys(&client); - MatrixClientUploadDeviceKeys(&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); - 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(); - - MatrixClientRequestMegolmInSession(&client, - ROOM_ID, - SESSION_ID, - SENDER_KEY, - USER_ID, - DEVICE_ID); - - // // decrypt room key - - MatrixOlmSession * olmSession; - MatrixClientGetOlmSession(&client, - USER_ID, - DEVICE_ID, - &olmSession); - static char encrypted[2048]; - static char decrypted[2048]; - printf("encrypted:"); - fgets(encrypted, 2048, stdin); - 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(); - - MatrixClientDeleteDevice(&client); - - MatrixHttpDeinit(&client.hc); - - return 0; -} diff --git a/examples/Keys.c b/examples/Keys.c deleted file mode 100644 index 17a681f..0000000 --- a/examples/Keys.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include - -#define SERVER "https://matrix.org" -#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO" -#define DEVICE_ID "MAZNCCZLBR" -#define USER_ID "@pscho:matrix.org" -#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" - -int -main(void) -{ - MatrixClient client; - MatrixClientInit(&client); - - MatrixHttpInit(&client, SERVER); - - MatrixClientSetAccessToken(&client, ACCESS_TOKEN); - MatrixClientSetDeviceId(&client, DEVICE_ID); - MatrixClientSetUserId(&client, USER_ID); - - MatrixClientGenerateOnetimeKeys(&client, - 2); - - MatrixClientUploadOnetimeKeys(&client); - MatrixClientUploadDeviceKeys(&client); - - char deviceKey[DEVICE_KEY_SIZE]; - MatrixOlmAccountGetDeviceKey(&client.olmAccount, deviceKey, DEVICE_KEY_SIZE); - printf("device key: %s\n", deviceKey); - - MatrixHttpDeinit(&client); - - return 0; -} diff --git a/examples/Login.c b/examples/Login.c index 08f981a..ffa4dd3 100644 --- a/examples/Login.c +++ b/examples/Login.c @@ -1,10 +1,11 @@ #include #include -#define SERVER "https://matrix.org" -#define USERNAME "pscho" -#define PASSWORD "Wc23EbmB9G3faMq" -#define DISPLAYNAME "MatrixClient" +#define SERVER "https://matrix.org" +#define USER_ID "@example:matrix.org" +#define USERNAME "" +#define PASSWORD "" +#define DEVICE_NAME "" int @@ -18,7 +19,7 @@ main(void) MatrixClientLoginPassword(&client, USERNAME, PASSWORD, - DISPLAYNAME); + DEVICE_NAME); printf("Access Token: %s\n", client.accessToken); printf("Device ID: %s\n", client.deviceId); diff --git a/examples/ReplyRoomkey.c b/examples/ReplyRoomkey.c deleted file mode 100644 index 7cc506e..0000000 --- a/examples/ReplyRoomkey.c +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include - -#define SERVER "https://matrix.org" -#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO" -#define DEVICE_ID "MAZNCCZLBR" -#define USER_ID "@pscho:matrix.org" -#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" - -int -main(void) -{ - MatrixClient client; - MatrixClientInit(&client); - - MatrixHttpInit(&client.hc, SERVER); - - MatrixClientSetAccessToken(&client, - ACCESS_TOKEN); - MatrixClientSetDeviceId(&client, - DEVICE_ID); - MatrixClientSetUserId(&client, - USER_ID); - - - MatrixClientSendEventEncrypted(&client, - ROOM_ID, - "m.room.message", - "{\"body\":\"Hello\",\"msgtype\":\"m.text\"}"); - - MatrixClientShareMegolmOutSession(&client, - USER_ID, - "ULZZOKJBYN", - &client.megolmOutSessions[0]); - - MatrixHttpDeinit(&client.hc); - - return 0; -} diff --git a/examples/Send.c b/examples/Send.c index 281d899..d5d27b9 100644 --- a/examples/Send.c +++ b/examples/Send.c @@ -1,8 +1,11 @@ #include -#define SERVER "https://matrix.org" -#define USER_ID "@pscho:matrix.org" -#define ROOM_ID "!koVStwyiiKcBVbXZYz:matrix.org" +#define SERVER "https://matrix.org" +#define USER_ID "@example:matrix.org" +#define ROOM_ID "!example:matrix.org" +#define USERNAME "" +#define PASSWORD "" +#define DEVICE_NAME "" int main(void) @@ -16,9 +19,9 @@ main(void) MatrixClientSetUserId(&client, USER_ID); MatrixClientLoginPassword(&client, - "pscho", - "Wc23EbmB9G3faMq", - "Test1"); + USERNAME, + PASSWORD, + DEVICE_NAME); MatrixClientSendEvent(&client, diff --git a/examples/SendEncrypted.c b/examples/SendEncrypted.c index 3debeab..749037d 100644 --- a/examples/SendEncrypted.c +++ b/examples/SendEncrypted.c @@ -1,9 +1,16 @@ #include #include -#define SERVER "https://matrix.org" -#define USER_ID "@pscho:matrix.org" -#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" +#define SERVER "https://matrix.org" +#define USER_ID "@example:matrix.org" +#define ROOM_ID "!example:matrix.org" +#define USERNAME "" +#define PASSWORD "" +#define DEVICE_NAME "" + +// device id of another device to share the megolm session with +// I used the device id of a logged in Element web session +#define DEVICE_ID2 "" int main(void) @@ -16,9 +23,9 @@ main(void) MatrixClientSetUserId(&client, USER_ID); MatrixClientLoginPassword(&client, - "pscho", - "Wc23EbmB9G3faMq", - "Test1"); + USERNAME, + PASSWORD, + DEVICE_NAME); MatrixClientUploadDeviceKeys(&client); MatrixClientGenerateOnetimeKeys(&client, 10); @@ -33,7 +40,7 @@ main(void) MatrixClientShareMegolmOutSession(&client, USER_ID, - "ULZZOKJBYN", + DEVICE_ID2, megolmOutSession); MatrixClientSendEventEncrypted(&client, diff --git a/examples/Sync.c b/examples/Sync.c index b503f13..8306990 100644 --- a/examples/Sync.c +++ b/examples/Sync.c @@ -2,15 +2,17 @@ #include #include -#define SERVER "https://matrix.org" -#define USER_ID "@pscho:matrix.org" -#define USERNAME "pscho" -#define PASSWORD "Wc23EbmB9G3faMq" -#define DISPLAYNAME "SyncClient" - -#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" -#define EVENT_ID "$_-y42DkC3OmJ_s40gYko7jMwrUQhoXfEut2pMV3E2J8" -#define SESSION_ID "tzdnJbDrm82D/RpgkZKpILTifQ5Rads+tVzp3ax8+Ls" +#define SERVER "https://matrix.org" +#define USER_ID "@example:matrix.org" +#define ROOM_ID "!example:matrix.org" +#define USERNAME "" +#define PASSWORD "" +#define DEVICE_NAME "" + +// event id of an encrypted event +// devices can only be verified after they used e2ee in some way +// (at least in Element) +#define EVENT_ID "$example" int main(void) @@ -25,7 +27,7 @@ main(void) MatrixClientLoginPassword(&client, USERNAME, PASSWORD, - DISPLAYNAME); + DEVICE_NAME); MatrixClientGenerateOnetimeKeys(&client, 10); MatrixClientUploadOnetimeKeys(&client); diff --git a/examples/Verify.c b/examples/Verify.c index c9680e0..7d5fc67 100644 --- a/examples/Verify.c +++ b/examples/Verify.c @@ -4,14 +4,20 @@ #include -#define SERVER "https://matrix.org" -#define USER_ID "@pscho:matrix.org" - -#define DEVICE_ID "ULZZOKJBYN" -#define SENDER_KEY "cjP41XzRlY+pd8DoiBuKQJj9o15mrx6gkrpqTkAPZ2c" -#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" -#define EVENT_ID "$vOS09eUaI0CduqAcaIU5ZVk6ljLQfLspz7UThP8vaUM" -#define SESSION_ID "90UbGLue3ADVhvW7hFjoA2c6yg0JJKs/lPdMDZXnZAk" +#define SERVER "https://matrix.org" +#define USER_ID "@example:matrix.org" +#define ROOM_ID "!example:matrix.org" +#define USERNAME "" +#define PASSWORD "" +#define DEVICE_NAME "" + +// event id of an encrypted event +// devices can only be verified after they used e2ee in some way +// (at least in Element) +#define EVENT_ID "$example" +// device id of another device to share the megolm session with +// I used the device id of a logged in Element web session +#define DEVICE_ID2 "" #define STATIC static @@ -25,9 +31,9 @@ main(void) MatrixClientSetUserId(client, USER_ID); MatrixClientLoginPassword(client, - "pscho", - "Wc23EbmB9G3faMq", - "Test1"); + USERNAME, + PASSWORD, + DEVICE_NAME); printf("deviceId: %s\n", client->deviceId); MatrixClientGenerateOnetimeKeys(client, 10); MatrixClientUploadOnetimeKeys(client); @@ -60,7 +66,7 @@ main(void) printf("megolm session id: %.10s... key: %.10s...\n", megolmOutSession->id, megolmOutSession->key); MatrixClientShareMegolmOutSession(client, USER_ID, - "ULZZOKJBYN", + DEVICE_ID2, megolmOutSession); // send 10 random messages